Bash Scripting Basics: Part 2 – Statements and variables


Now that we know how to create a very basic script and run them its time to get started on learning how statements and variables work. Every language has itsown way of using statements and variables, they might look slightly different when comparing one language to another, but they all have the same goal in mind. That makes bash scripting the ideal language to use as a stepping stone onto other programming languages, because once you understand the basics of programming you can easily work up to more difficult languages.



If you have ever taken any classes in algebra you should already know what a variable is. A variable in algebra is used represent an unknown number. So for example if you say x + 8, if the sum equals 10 then you know x is equal to 2. Variables can be used similarly in bash for both numbers and strings. Let’s take a look at how we can find the solution to our above algebraic equation in bash scripting.



x=$(($total – 8))

echo “The value of x is $x”{/codecitation}

As you can see we know that the total is 10, so we can assign 10 to a variable called total. We then create a variable called x which is used for calculating the value of x by calculating the total minus 8. As you can see when a variable is called anywhere in the code, it is assigned a dollar sign to show that it is a variable, similar to PHP. An arithmetic expansion “$(( [expression] ))” is then used on the variable which allows us to calculate the two numbers. Finally the value of x is then printed inside our string using echo to show the result. You may want to note that any command that can be used from the shell (not just echo) can be used inside your bash script.

In bash scripting variables come in two shapes, you have a global variable (which is what we have seen above) and you also have a local variable which are used inside functions. Global variables can be used anywhere throughout your script including inside functions, however a local variable is a variable that is assigned inside a function and cannot be used outside of that function. We will discuss functions a little later on it but it’s a good idea to keep that in mind.

If statement

The if statement is probably the most commonly used statement used throughout all languages. It is called a conditional statement because it relies on another condition to determine whether it is true or false, much the same as how our brains work.

Think about an if statement in the following way. Say for example Saturday is the day you go shopping. You would think to yourself, if today is Saturday, hop into your car and go buy the groceries from the store, else do not go to the shops. We can create if statements in bash much the same way.



if [ "$today" == "Saturday" ]; then

echo “Go shopping”


echo “Go to work”


We first create our variable which is today. We then place an if statement in which compares the variable $today to the word Saturday. This is done by using an operator such as “==” equals or “!=” does not equal. If today equals Saturday, the statement comes back as true, then we echo “Go shopping”. Else if today does not equal Saturday and the variable is Friday, the statement would come back as false and “Go to work” would be echoed. We then close our statement off with fi, in some other languages you would use brackets instead.

We can also extend the statement to check for other conditions, we can do this using the elif (else if) statement. This is called a nested statement. As you can see below if we change the day to Sunday and add an elif statement which also checks to see if today is Sunday, then we can echo “Go to the park”.



if [ "$today" == "Saturday" ]; then

echo “Go shopping”

elif [ “$today” == “Sunday” ]; then

echo “Go to the park”


echo “Go to work”


While statement

The while statement is another conditional statement much the same as the if statement except it is used for creating a loop. If you can imagine the scenario above in which each day of the week you would perform a certain task at work. If you had to repeat that task over and over you would create a loop and so you would say, while a certain condition is true continue to loop until that condition is false.

To make it easier, let’s say our fictional guy works at as a lumber jack and he is required to chop wood for 10 hours a day between 8am and 6pm. We could say, if the time is between work hours then return true and chop wood. But if the time does not equal work hours then stop chopping wood. Let’s take a look at how we would do that in bash scripting.



while [ $hours -le 24 ]


echo $hours

if [ $hours -ge 8 ] && [ $hours -lt 18 ]; then

echo “chop wood”


let hours=hours+1


We start off by declaring a variable “hours” with a value of zero, this variable will be our time in 24 hour format. We then create a while statement a loop which states while the hours are less than or equal to “-le” 24, then perform our action.

We then combine two if statements together using the and (&&) operator and say if the hours are greater than or equal to 8 and if the hours are less than 18 (8am – 6pm), then echo chop wood. We then place at the end of our loop a statement which sais let hours equal hours plus one, which increments the hours variable by one each loop.

Originally posted 2016-01-11 05:53:27.


Please enter your comment!
Please enter your name here