Raspberry Pi SH (Bash) File: Install, Create and Run Shell Scripts

A .sh (shell) or bash file can be utilized in increasing productivity and managing menial, repetitive tasks. When a script has properly set permissions and syntax it then executes commands in a fraction of the time that you would take. In this post, we will create and run a shell (.sh) script on Raspberry Pi 3/4. We will also include 3 Raspberry Pi bash script examples that you can use as a starting point.

With Shell Scripts you can automate anything you can do on the Linux command line. It is a file that carries shell commands executed one by one as they were input on the command line directly.

The best benefit is they help save time by executing many commands at the same time or if you intend to automatically boost commands like pi boots up.

2 most common advantages of using a shell script on your Raspberry P:

  1. Automate home tasks or any hardware your Pi may be attached to.
  2. With this write down a script to automate a set of instructions that need to be executed one after the other despite typing in commands one after the other.
  3. Automate repetitive tasks, create accounts for different users, and much more.
  4. Data Backup: would you want to run your backups manually at 2 in the morning? No.

How does an SH file or Bash Script Work?

In the realm of Linux and systems in general there is the concept of programs and processes. The program usually carries binary data which consists of a series of instructions for the CPU and other resources which include pictures, sound files and much more, which are organized in the form of a package and typically stored there on the hard disk.

Now, when you are running a program it is not merely the program it is indeed a copy of it known as a process. The instructions and resources are copied from the hard disk into RAM or working memory. Moreover, some space is also allocated in RAM for the process to store variables and some flags to allow the OS to manage and track the complete process during its execution.

The Bash Process

Now, chances are that many different processes can represent the same program running in the memory simultaneously. You can open two terminals and run the cp command in both. In such a case, two cp processes will be currently present on the system. As they finish running the system shall destroy them and no processes shall be there to represent program cp.

Now, when you are at the terminal you need to have the bash process running to have the bash shell. What if you start running a script? Well, it won’t run in that process but shall start a new process to run within.

ALSO SEE: How To Setup Raspberry Pi Remote Desktop Connection to Windows 10/11.

How To Create and Run Your Own .sh Shell Script on Raspberry Pi 3/4 (With Examples)

If you are the kind of person who enters long sequences of commands into the command line then this is designed for you. If you know how to write a shell script, many operations can be accomplished with only a single command. It can be used to:

  1. Batch convert file formats.
  2. Rename batch and move files.
  3. Use one command output as the input of another command.
  4. Whenever an event occurs it starts the applications.
  5. Have control over what happens whenever the system boots up.

Among many features, one of the best is its ability to create pipes. For those who are unaware, this allows the output of one command to be forwarded to the input of the next one. Moreover, they can also be used with many commands. Its basic syntax is as follows an example:

Run First Backup | Run Offsite Backup

Create a .sh bash Script File in 5 Steps

These are executable text files with the extension “.sh”. We are going to explain to you with examples, we shall be writing a simple “hello world” script to explain how to create and run them.

  1. Login to “Raspberry Pi”.
  2. Then navigate to the directory where you wish to save the script, e.g. /home/paul/scripts.
  3. After this open “nano or vim text editor” and create a new file, which shall be named “hello-world-from-ninja-ide.sh” by entering the following command:
Paul@ninja-ide:~# vi hello-world-from-ninja-ide.sh

4. After this, enter the following code in the text editor.

Echo “hello world from ninja-ide!”

The first line is called shebang, which tells the BASH shell to execute the command there in the script. Each script created shall need this on the first line.

5. Exit and then save the file in Nano by hitting “Ctrl-X” from the keyboard to save and exit.

Now you need to use chmod:

Paul@ninja-ide:~# chmod u+x hello-world-from-ninja-ide.sh

Running your Shell Script

Running a Shell Script

Navigate to the directory where the file has been saved and enter the following command:

Paul@ninja-ide:~# cd /home/paul/scripts

Once you are in the directory, just put ./ in front of the script and run it.

Paul@ninja-ide:~# ./hello-world-from-ninja-ide.sh

That is all, you have now successfully run your bash script on Raspberry Pi.

ALSO SEE: 14 Open Source and Free Python Scripts For Ethical Hacking.

3 Examples of Bash Script for Raspberry Pi

Bash Script Examples for Raspberry Pi

Command delay script

If you wish to pause any command execution for a certain time then you can use this command, simply set the delay amount by seconds, minutes, hours, and days. For this create a file named “pause-command-runninh.sh” and add the script. Ti shall wait for around five seconds after running.

Automatic Backup

You can create automatic backups for example if you are running your own web server or Plex server. This can be done by scheduling your script or using Cron jobs.

Delete Data

To remove any file use the “rm” command. For this create a file by the name “automatic-deletions.sh” with code to take the name of the file from the user and remove it. This can be used for things such as kill switches or if 3 or  4 password attempts fail, it self-destroys.

Custom projects

If you are a student and require a project, you can use shell scripts and a Pi to make an interesting weather prediction project. You can take it a step further, apply AI/ML models and datasets, and use bash scripts to execute commands at different times of the day.


Raspberry Pis are great low-power and budget computers; these are more capable than you think they are! This article detailed all you need to know about Raspberry Pi sh file and bash file and how to create and run them. We also shared 3 bash script examples that you can run on your Raspberry Pi to have some automation. Happy scripting!

Previous articleHow To Install, Create and Run Python Programs on Android
Next articleHow To Create a Simple HTTP Python Web Server (With Examples)
Paul Carruthers
Paul is an avid programmer who specializes in Python and Java with over 16 years of experience in the field. He loves automating complex tasks and creating useful scripts to streamline work and make life easier. He is also a massive fan of Linux and currently uses it as his main desktop OS. When he is not staring at code, he loves hiking and swimming in different parts of the world.


Please enter your comment!
Please enter your name here