In this tutorial I’ll discuss the basics of shell scripts, what they’re used for, and how to write and run them on the Raspberry Pi.
What is a Shell Script?
Shell scripts allow you to automate pretty much anything you can do on the Linux command line. A shell script is simply a text file containing a series of shell (command line) commands that are executed one by one, as if they were input directly on the command line. They can save time if you need to execute lots of commands at once, or if you want to execute commands automatically, like when the Pi boots up.
The shell language is a high level programming language. It’s more removed from system and memory processes than lower level languages like assembly or C. This basically means that it has commands and functions to perform tasks that would otherwise take a lot of code to execute.
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. For example, they can be used to:
- 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 a shell script 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.
Create the Script File
Shell scripts are simply an executable text file with the extension “.sh”. In this example we’ll write a simple “hello world” script to demonstrate how to create and run them.
To begin, log in to your Raspberry Pi, and navigate to the directory where you want to save the script. Then open the Nano text editor and create a new file named hello-world.sh by entering this at the command prompt:
sudo nano hello-world.sh
Now, enter this code into the 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. Every shell script you create will need this on the first line of the script.
Exit and save the file in Nano by pressing Ctrl-X to save and exit.
Make the File Executable
Next, we’ll need to make the hello-world.sh file executable. To do that, enter this at the command prompt:
sudo chmod +x hello-world.sh
Run the Script
Now that the shell script has been made executable, we can run it. Navigate to the directory where the file is saved, and enter this:
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, let us 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 – we send out an email when we publish new posts…
+ Bash is a square and shell scripting is rectangle. Shell scripts could be; #!bin/ sh, ksh, csh, tcsh, bash, or zsh.
Your article is fine but just like other articles on scripts, it falls short. In your article you list what they can be used for but you leave us hanging. You should have included information and an example on how to do the following on a Raspberry Pi:
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
The article wasn’t called “How to do things on your Raspberry Pi”
No it wasn’t but the graphic shows a script with code accessing GPIO pins and implies that you will explain more advanced use of scripts. You could have explained that code. I can write code to print “Hello World” and that is why your article is no different than the 100’s of others on using scripts.
This article is exactly what it is titled and was an excellent help to me.
oh Fred….
Thank you!
atom
Great article which made sense. So many articles on this are so so complex. This was much clearer. Thank you.
Hello admin. Can you share the right design of having an good quality of tv antenna?
Very good suggestions, you just gained a brand new reader. What would you suggest in regards to your post that you made a few days ago?
Jimmy Choo shoes http://www.jimimychoo.com
Nice content you got here for your site, any suggestions?
Designer fashion http://www.hervelegger.com
Would it be possible to use a shell script to open an application, select a file to run in the application, and execute the selected file in the application with one click to start the shell script? This would prevent me from having to open my application, browsing for the file I want to run, and then pressing an execute command button.
hello everyone, new to this raspberry thing. its interesting. i’m attempting to write a script that uses fswebcam to take pics from two usb cameras – /dev/video0 and /dev/video1. it needs to be at intervals, lets say every 15 minute, 1 of the cameras takes a pic then the next one 15 minutes after. so, really 1 camera takes 1 pic every 30 minutes. and I would like to store the jpg files in a specific dir. I would also like to initiate at boot up of the system. can anyone offer any direction on how to accomplish. many thanks.
Great explanation! Sir, I am working with configuring OP-TEE on raspberry pi 4 to use it as a protection against the privilege escalation attacks but I’m a beginner to this RPi. As an alternative to this I have got an idea to sun a shell script to run it at the first boot up of the device after referring this blog and Hope that you can help me with this Sir!
Hi,
I am trying to execute a script that will install and enable i2c on a rpi 4b
https://gist.github.com/grubernd/aed721614b36aaa31fd97ef5ab1ec6be
What command do I use to run it. Can I grab it from github and execute/install it with one command.
Thank you.