In this tutorial we will provide basic examples of the conditional statements in bash scripting.
Passing Variables to Scripts via Command Line
In the script, we can define the variables with the dollar sign $
. By adding a number, we are referring to the variable that we want to pass. So, the $1
is the first variable, the $2
is the second variable and so on. Let’s see an example. Let’s consider the following myexample.sh
script.
echo "Hello, I pass the first variable $1 and the second one $2"
And let’s run it by typing:
bash myexample.sh var1 var2
Here, the var1 is assigned to $1
and the var2 to $2
and the output is:
Hello, I pass the first variable var1 and the second one var2
The if-then-else Statement
Below we provide a list of Bash Conditional Expressions
-a file | True if file exists. |
-b file | True if file exists and is a block special file. |
-c file | True if file exists and is a character special file. |
-d file | True if file exists and is a directory. |
-e file | True if file exists. |
-f file | True if file exists and is a regular file. |
-g file | True if file exists and its set-group-id bit is set. |
-h file | True if file exists and is a symbolic link. |
-k file | True if file exists and its “sticky” bit is set. |
-p file | True if file exists and is a named pipe (FIFO). |
-r file | True if file exists and is readable. |
-s file | True if file exists and has a size greater than zero. |
-t fd | True if file descriptor fd is open and refers to a terminal. |
-u file | True if file exists and its set-user-id bit is set. |
-w file | True if file exists and is writable. |
-x file | True if file exists and is executable. |
-G file | True if file exists and is owned by the effective group id. |
-L file | True if file exists and is a symbolic link. |
-N file | True if file exists and has been modified since it was last read. |
-O file | True if file exists and is owned by the effective user id. |
-S file | True if file exists and is a socket. |
file1 -ef file2 | True if file1 and file2 refer to the same device and inode numbers. |
file1 -nt file2 | True if file1 is newer (according to modification date) than file2, or if file1 exists and file2 does not. |
file1 -ot file2 | True if file1 is older than file2, or if file2 exists and file1 does not. |
-o optname | True if the shell option optname is enabled. The list of options appears in the description of the -o option to the set builtin (see The Set Builtin). |
-v varname | True if the shell variable varname is set (has been assigned a value). |
-R varname | True if the shell variable varname is set and is a name reference. |
-z string | True if the length of string is zero. |
-n string | True if the length of string is non-zero. |
arg1 OP arg2 | OP is one of ‘-eq’, ‘-ne’, ‘-lt’, ‘-le’, ‘-gt’, or ‘-ge’. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to arg2, respectively. Arg1 and arg2 may be positive or negative integers. When used with the [[ command, Arg1 and Arg2 are evaluated as arithmetic expressions (see Shell Arithmetic). |
Let’s provide an example of a simple “if statement”. The script ifthenelse.sh
is the following and it asks the end-user to enter a number between 30-40 and if the guessed number is equal to 37 (-eq
) then it returns “You found it!“, else it returns “Wrong answer!“:
echo "Guess my Age!" echo "#####################" echo "" echo "Enter a Number Between 30 and 40: " read GUESS if [ $GUESS -eq 37 ] then echo "You found it!" else echo "Wrong answer!" fi
Let’s run it by guessing the correct age which is 37 and then again by entering 35.
Note that the else if in Bash scripting is elif
as in Python.
The for loop
Let’s say that we want to get the first line of each Bash script in our working directory:
echo "List the first line of all bash scripts within the directory" SHELLSCRIPTS=`ls *.sh` for SCRIPT in "$SHELLSCRIPTS" do DISPLAY="`head -n 1 $SCRIPT`" echo "File: $SCRIPT - First Lines $DISPLAY" done
The Case Statement
Let’s provide an example of the case-statement in Bash:
echo "How shall we proceed" echo "+++++++++++++++" echo "Press 1 for the main menu" echo "Press 2 for help" echo "Press 3 to exit" echo "" echo "Press a number between 1 and 3" read CHOICE case $CHOICE in 1) echo "You are in the main menu";; 2) echo "You asked for help";; 3) echo "You exit";; *) echo "Choose a number between 1 and 3";; esac
The while loop
Let’s provide an example of a while loop.
echo "This is a while loop example" COUNT=1 while [ $COUNT -le 10 ] do echo "This is the loop number $COUNT" COUNT="`expr $COUNT + 1`" done
Execution Operators (&& and ||)
In Bash scripting the AND is &&
and the OR is ||
.The right side of &&
will only be evaluated if the exit status of the left side is zero (i.e. true). ||
is the opposite: it will evaluate the right side only if the left side exit status is non-zero (i.e. false). If the test inside evaluates to true, it returns zero; it returns nonzero otherwise. For example:
$ false && echo howdy! $ true && echo howdy! howdy! $ true || echo howdy! $ false || echo howdy! howdy!