How to Write and Run a Shell Script on the Raspberry Pi

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.

BONUS: Download the Raspberry Pi programming cheat sheet – a one page PDF guide with instructions on how to create and execute C programs, Python programs, and Shell scripts.

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…