An introduction to BIN Bash-ing on Raspbian

An introduction to Bash shell
Published: by
Last updated:
Categories
Raspbian

The Raspberry Pi is a great stand-alone device that has the ability to use Bash scripts. Bash scripts allow multiple commands to be executed one by one.

Introduction

An introduction to BIN Bash-ing on Raspbian explains some very basics of Bash script files, how to create and edit them, set their privileges, where to store the files and ultimately how to execute them.

In the process, it will also show how to work with Linux directories, move, duplicate and remove files and how to create and remove directories. For a summary of all these commands, see the bottom of this post.

Bash shell vs Bash scripts

Bash shell, or simply Bash, is a standard shell for Linux users. When using most Linux systems, including Raspbian, the operating system’s user commands can also be in the form of typed commands in the terminal. After each command is typed the Enter key is pressed for it to be executed. These commands and how they are interpreted by the system are called Bash. Commands include the execution of files (e.g. running programs, Python or Bash files or Bash scripts).

A Bash script consists of one or more Bash commands. Anything that can be executed in the command terminal can be placed into a Bash script. Each line of the Bash scrip represents an Enter key. Bash scripts are saved as .sh files. Bash commands inside a script will be run sequentially.

Both Bash and Bash scripts can be scheduled to be executed by Cron.

To sudo or not to sudo

By default, Raspbian is configured to have one ‘superuser’. This is usually the user type that most Raspberry Pi users will be using (after logging in with pi:raspberry at the command prompt). By following the rest of this post it is assumed that the superuser is used.

Raspbian terminal
Raspbian terminal after the user has logged in

Creating Bash scripts

Bash scripts are written as text files containing individual Bash commands. Each command is written as a separate line. Bash scripts are saved as .sh files. Before creating the Bash script, make sure to be in the default working directory by using the following command:

cd /home/pi

Nano can be used to create the file. By typing nano and the name of the file to be created, Nano will either open the file if it already exists or create the file if it doesn’t:

nano helloworld.sh

In this case, the file will most likely not exist and Nano will open a blank file.

Creating a Bash script

To start editing the Bash script file, add the ‘shebang’ as the first line:

#!/bin/bash

The shebang should be added as the first line of all Bash scripts. The /bin/bash part is the interpreter. This tells Raspbian system that it is a Bash script and what shell to use – in this case Bash.

The hash sign (#) at the beginning of the shebang tells Linux not to see it as code, but because it is the first line it will still attempt to identify the interpreter.

A more traditional use for the # is as a commenting marker. In Linux, hashes is used to precede notes or to temporarily deactivate commands. Now, add the following commenting marker to the second line of the script:

# This is a Bash script to say hello to the world.

To save the Bash script, press and hold Ctrl+x, and then press y. For now, use the given name and directory for the file to be saved in (i.e. press Enter).

Saving a Bash script for the first time
When asked what to save the file as, just press Enter.

The directory structure

In order to keep things organised, files on an operating system are stored in directories. On the Raspberry Pi running Raspbian, the default user directory is /home followed by the username, in this case as /pi. Each time the system boots, Raspbian will start in this (/home/pi) directory. When the user is inside this directory it will not show on the terminal.

When a command is given, Raspbian will assume to use the directory the user is currently in. At any time, the user can use the pwd command to see where the command will be triggered from.

After the Bash script has been saved, the user will be returned to the terminal. By default, Nano will prompt to save files in the current working directory. In this case it was /home/pi. To list all the files in this directory, the dir command can be used. This will show the newly created helloworld.sh.

Raspbian 'dir' command
To see the listed contents of a directory, use the ‘dir’ command.

Creating new directories

Any directory within /home/pi can be used, but many Linux users like to use the /bin directory for their Bash script files. To create a new directory the /mkdir command, followed by the directory name, is used. Use this command as follows to create a /bin directory inside the/home/pi directory:

mkdir /home/pi/bin

By using the dir command again from the /home/pi directory, the /bin directory, together with helloworld.sh file, will be listed.

Raspbian 'mkdir' command

Moving to a directory

To move to a directory, the cd command, followed by name of the directory, is used. To move to the newly created /bin directory, the cd command should be used as follows:

cd /home/pi/bin

By using the cd command on its own (i.e. without the name of a directory), the user will be taken back to the default user directory (in this case, /home/pi).

The absolute directory structure

In order to prevent confusion, rather get into the habit of using the full, or absolute directory structure. Directory related commands (e.g. dirmkdircdmvcprm) can also be used with the relative directory structure. If for example the mkdir command was used with /bin instead of bin (note the forward slash), Raspbian would have attempted to create a /bin directory in the root of the operating system and not /home/pi/bin. By using the absolute directory structure the need to keep track of the current user directory also becomes less important.

Moving or copying files between directories

Files can be moved or copied from one directory to another by using the mv or cp commands. The mv command will copy the file to a target directory and will automatically delete if from the source directory – similar to cut and paste. The cp command will copy the file to a target directory without deleting the if from the source directory.

Both these commands are followed by the file to be moved or copied (including the target directory) and the destination directory:

mv /targetdirectory/file.sh /destinationdirectory

or

cp /targetdirectory/file.sh /destinationdirectory

To move helloworld.sh to the newly created bin directory, the command is used as follows:

mv /home/pi/helloworld.sh /home/pi/bin

To see if the file has been moved, use the following two dir commands:

dir /home/pi
dir /home/pi/bin

Raspbian 'mv' command

Editing files

Now that the Bash script has been moved to the /home/pi/bin directory, for our purposes it is ready to be updated with more commands. To open the Bash script file again, use the nano command again followed by the file to be edited – in this case helloworld.sh:

nano /home/pi/bin/helloworld.sh

To illustrate, the echo command will be used, but by doing some searching on the internet, any Bash command can be used. In Bash, the echo command is used to print some desired text on the screen. Add the following line to the Bash script:

echo "Hello, world..."

Here, the echo command, will display the text between quotation marks on the screen – Hello, world… The quotation marks are to identify the beginning and the end of the text to be displayed and will not be ‘echoed’.

Press and hold Ctrl+x, and then press y to save the file again.

Running Bash scripts

Now that the Bash script contains all the desired commands, it is ready to be executed. Bash script files can either be executed from the terminal or from Cron using the bash command. To run the script, use the bash command as follows:

bash /home/pi/bin/helloworld.sh

Raspbian 'bash' command

Removing/deleting a file or directory

When files or directories need to be removed, the rm and rmdir commands is used. The rm command is used to delete a file and the rmdir command is used to delete a directory. Similarly to all the other directory-related commands mentioned earlier, these commands are trailed by either the file name or the directory to be deleted.

To delete helloworld.sh the rm command can be used as follows:

rm /home/pi/bin/helloworld.sh

The rmdir command is slightly different from the other directory-related commands in that only empty directories can be deleted and that the user cannot be inside the directory while attempting to delete it (sometimes).

The cd command can be used to move to a different directory before it is deleted. To delete the /home/pi/bin directory the /rmdir command can be used as follows:

rmdir /home/pi/bin

Raspbian command line summary

Create and Edit a file:

nano /home/pi/bin/helloworld.sh

Create a directory:

mkdir /home/pi/bin

Move a file:

mv /home/pi/helloworld.sh /home/pi/bin

Duplicate a file (copy/paste):

cp /home/pi/helloworld.sh /home/pi/bin

Remove a file:

rm /home/pi/helloworld.sh

Remove a directory (needs to be empty first):

rmdir /home/pi/bin

Execute a Bash script:

bash /home/pi/bin/helloworld.sh
About the author
Renier busies himself with improving his English writing, creative web design and his websites, photoshopping, micro-electronics, multiple genres of music, superhero movies and badass series.
Behind the Scenes is a free, informative website. If you find value in any of our content, please consider making a donation to our cause, or becoming a Patron for exclusive content.
Donate via PayPal Become a Patron

Save, share & discuss

Your comment is important, but don't be a knob. Keep it constructive and polite.

Leave a Reply

Your email address will not be published. Required fields are marked *

More Raspbian related posts