In this tutorial, I’ll discuss the basics of what a shell script is, what they are commonly used for, and finally, how to write and run a shell script on the Raspberry Pi.
What is a shell script?
A shell script is simply a text file containing a series of shell (command line) commands that are executed by the BASH shell. The shell reads the script file and executes the commands one by one, as if they were input directly on the command line. This can save time if you need to execute many commands to complete a task, since all you need to do is create a shell script and run it. Shell scripts allow you to automate pretty much anything you can do on the Linux command line.
The shell language is a high level programming language. It is more removed from system and memory processes than lower level languages. This basically means that it has commands and functions to perform tasks that would otherwise take a lot of code to execute in lower level languages like C or assembly language.
Shell scripts can make use of variables, if-then statements, loops, and pipes (see below).
What can a shell script do?
Shell scripts are great if you need to enter long sequences of commands into the command line to do something. Most operations can be accomplished with a single command if you know how to write a shell script for it. Some things they can be used to do:
- Control what happens when the computer boots up
- Start applications when an event occurs
- Use the output of one command as the input for another command
- Batch rename and move files
- Batch convert file formats
One very useful feature of shell scripts is the ability to create pipes. A pipe allows the output of one command to be forwarded to the input of the next command. Pipes can be used with as many commands as you want. The basic syntax for a pipe is:
command 1 | command 2
How to create and run a shell script
I’ll just cover the basics of writing shell scripts here, but the book Classic Shell Scripting by Arnold Robbins is an excellent resource for anyone interested in learning the details about writing shell scripts.
Shell scripts are simply an executable text file with the extension “.sh“. In this example we will write a simple “hello world” script.
To begin, log in to your Raspberry Pi, and navigate to the directory where you will save the shell script. Then enter sudo nano hello-world.sh at the command prompt to open the nano text editor and create a new file named hello-world.sh. Now, enter this code into the nano text editor:
#!/bin/bash echo "Hello World!"
The first line of this program, #!/bin/sh, is called a shebang. This tells the BASH shell to execute the commands in the script.
Exit and save the file in nano by pressing Ctrl-X to save and exit.
Next, we will need to make the hello-world.sh file executable. To do this, enter sudo chmod +x hello-world.sh at the command prompt.
Now that our shell script is executable we can run it. Navigate to the directory where the file is saved, and enter either sh hello-world.sh or ./hello-world.sh.
The words “Hello World!” will be printed to the line below the command prompt.
This “hello world” script isn’t particularly useful, but it will show you the basics of how to create and run a shell script. For a list of some really useful Linux commands you can use in your shell scripts, see our article 42 of the Most Useful Raspberry Pi Commands.
If you have any questions or problems with anything in this post, please let me know in the comments! Thanks for reading, and if you know anyone that could benefit from this information, please share it! You can also keep updated on our new posts by subscribing – just enter your email in the subscribe box below…
Need an easy-to-use way to design circuits and layout PCBs?
Try EasyEDA, a free circuit design software that also offers low cost, high quality PCB manufacturing.