Linux Bash Shell Script Programming

The Linux system uses a shell called BASH (Bourne-again Shell) which is very powerful and combined with the power of the operating system almost worth being called a programming language of its own. Its syntax is different from most programming languages however and typical of a scripting language. This document gives you the basics.

To create a script use any plain text editor and name the file extension .sh as in “” and then change the permissions using the terminal or GUI environment you are using for Linux so that the file is executable (the terminal command would be “chmod +x”)

Once it is made executable from the terminal you can execute it from its resident directory with “./” or whatever.

Or if you have a GUI environment you can change the permissions there by a right click and going to the properties and the permissions tab and checking the box that says execute: allow to execute this file as a program.

The syntax of the scripting language follows…

Begin all script files with “#!/bin/bash” to indicate it is a bash file. This is a comment so it has no effect and is not required, but it is good practice. Example.


Comment out lines – preface with “#!” as in the previous example.

output to screen – “echo” as in example:

echo Hello World

redirecting output to a file – use “>” as in example:

ls > listing_of_this_directory.txt

record error output – use “2>” as in example searching for the word mickeymouse.

grep mickeymouse * 2> errors.txt

Pipes: Piping is similar to directing but it allows you to use the output of one program as the input in another instead of just writing it to a file. The piping symbol is “|” which is above your enter key. An example that would list all files in a directory and search those file names which contain the letter a…

ls | grep “a”

Variables: When using scripting and programming languages you have to know how to declare a variable. The equals symbol is all that is needed to declare a variable, but there can be no space between the name of the variable, the equals symbol, and it’s value. String variables are put into quotes like…

MYSTRING=”This is my string.”

Then you can use reference the variable’s value with the dollar sign like…


Numerical variables are just as easy…



Variables do not need to be placed in all caps, but it’s good practice.

If you need to add the value of a number or do some other math you use the keyword “expr” inside of a backquotes (the key above your tab key). Or you can use double parenthesis with a dollar sign as needed which is less verbose. Examples.

echo `expr $MYNUMBER + $MYNUMBER`

echo $(( 5 + 5))


or you can assign a value to a variable using an expression or an expression referencing a variable’s value also like this…


Pretty simple.

Now as with regular programming languages you can write bash scripts to segment part of your code from others with the braces to create functions/methods. Like…

echo This part is separate…
function myfunction { echo from this part…
echo unless this function is envoked…
echo as it is on the last line…
echo this message will never display

Any variables defined within a separate function however remain global unless declared as local. The syntax for a local variable is like this…

local MYMESSAGE=”This is my message declared as a local variable for use in a single function”

Now that you understand function declarations and how to call one we can go onto program flow which includes if, then, and else statements, for, while, and until, and case choices, etc.

For if statements we put the expression in brackets and follow that with a terminator semi-colon followed by the keyword “then”. The following lines are executed until we reach the reversed “if” keyword which is “fi”. If we need an else statement it follows whichever lines we wanted executed if the statement were true but before the word “fi”. Pretty simple.

if [ $A = $B ]; then

echo A is equal to B


echo A is not equal to B


Now we’ll cover for, while, and until statements. The for statement is not like others you find in programming. In bash scripting it is used for passing an operation by particular words in a string. An example from is…

for i in $( ls ); do
echo item: $i

The explanation given is a little cryptic so I’ll try to explain better. The keyword “for” comes first. Then the variable “i”. Using the letter “i” is not necessary but is a typical naming convention in programming for anything that increments. The word “in” I assume is used in place of “input” and “$( ls )” means the listing in a directory is going to be used as a variable. The directory listing will act something like an array so that we can pass over each item within it an increment the value of i as we do something to each item (which is each file listed in the directory) that is stored in our variable “i”. The value of “i” begins with the first filename within the directory listing. It first gains this value and then executes the code which is just to list the filename with the word “item:” in front of it. Then it loads the next value into “i” which is the next file name incrementing through the list. In this way “for” in bash is a different than in other programming languages for the value of “i” is not a number which increments, but a variable that can store any single item in a list of things but only one at a time. For this cause it can be used in a similar way to arrays in other languages and in a similar way to the keyword “for” in other programming languages being something like them both, but being more memory efficient as not as much space is needed as in an array, but the functionality of loading values sequentially into a variable as you could with an array is achieved without one. At least that’s my understanding of it.

The author of also has an interesting code sample where the keyword “for” is used more like how it would be in C. He writes…

“fiesh suggested adding this form of looping. It’s a for loop more similar to C/perl… for.

for i in `seq 1 10`;
echo $i

That’s novel and worth study.

Now we’ll discuss the “while” keyword. It functions pretty normally doing regular increments. Example:


while [ $INCREMENTthis -lt 5 ]; do

echo All work and no play makes Jack a dull boy.



We are just saying while the value of the variable is less than (-lt is the abbreviation in bash for less than) the value 5 do something and then increment by one. The keyword done must follow the end of the do statement. Spacing is very important in these kinds of statements. There must be a space separating the brackets from the internal statement and a space after the semicolon, etc. Check that stuff carefully.

The until statement is similar…


until [ $MYNUMBER = 0 ]; do




The only thing new there is the expression “-=” which means subtract the value of the variable by one. To add we just use the += etc. This is a typical incremental thing used in programming. In other languages it sometimes looks like “++” or “–” etc. if you always want to use the number 1 as your incrementing factor.


A great script from the user “Indra” at switches the Ubuntu desktop background. Indra writes, ”


PIC=$(ls $DIR/* | shuf -n1)
gsettings set org.gnome.desktop.background picture-uri "file://$PIC"

Save this script and edit your with the command “crontab -e” (it launches an editor where you put this line at the end of the file):”

Great job Indra! It worked for me.