Raspberry Pi bash Scripting

By | November 2, 2018

Raspbian’s default shell environment is bash. What does that mean? It means that the command line environment you use on the Raspberry Pi (assuming you are running Raspbian) speaks bash. Other shells are installed on the Raspberry Pi but you have to specifically call them. A good first bash script to write is the typical “Hello World” script.

The first line of the script is the shebang line. It is a #! immediately followed by the complete path to the executable used on the program. In our case it is a bash script and bash is inside the bin directory. The next line is a comment; they always start with a # (hash). The echo command prints the text after it to the screen. The exit command terminates the script

#This is a hello world script.

echo Hello world


Using the nano editor, type your code into a file for execution. Note that the dollar sign is my way of representing a command prompt and everything after it represents what you should type.

$ nano hello.sh

This screenshot shows the script in nano.

Bash Script in nano.

The script needs to be made executable before it is executed. Run the chmod command on the script to do so.

$ chmod +x hello.sh

Now use ls -l to list the files and directories.

$ ls -l

Directory Listing

On the left side and across from hello.sh you should see the same string of underlines and letters as shown below. Most importantly that string should contain three x’s. I have left out some of the details but cover them in my post on making files executable.

The script can be executed in two ways shown below. You must be in the same directory as the script for these exact commands to work.

$ ./hello.sh
$ bash hello.sh

This screenshot shows the results from both methods of execution.

Hello.sh Execution

Leave a Reply

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