Bash conditional if and case statements

Bash conditional if and case statements

At times you need to specify different courses of action to be taken in a shell script, depending on the success or failure of a command. The if construction allows you to specify such conditions.

The most compact syntax of the if command is:


The TEST-COMMAND list is executed, and if its return status is zero, the CONSEQUENT-COMMANDS list is executed. The return status is the exit status of the last command executed, or zero if no condition tested true.

The TEST-COMMAND often involves numerical or string comparison tests, but it can also be any command that returns a status of zero when it succeeds and some other status when it fails. Unary expressions are often used to examine the status of a file. If the FILE argument to one of the primaries is of the form /dev/fd/N, then file descriptor “N” is checked. stdin, stdout and stderr and their respective file descriptors may also be used for tests.

Expressions used with if

The table below contains an overview of the so-called “primaries” that make up the TEST-COMMAND command or list of commands. These primaries are put between square brackets to indicate the test of a conditional expression.

Primary expressions

Primary Meaning
[ -aFILE ] True if FILE exists.
[ -bFILE ] True if FILE exists and is a block-special file.
[ -cFILE ] True if FILE exists and is a character-special file.
[ -dFILE ] True if FILE exists and is a directory.
[ -eFILE ] True if FILE exists.
[ -fFILE ] True if FILE exists and is a regular file.
[ -gFILE ] True if FILE exists and its SGID bit is set.
[ -hFILE ] True if FILE exists and is a symbolic link.
[ -kFILE ] True if FILE exists and its sticky bit is set.
[ -pFILE ] True if FILE exists and is a named pipe (FIFO).
[ -rFILE ] True if FILE exists and is readable.
[ -sFILE ] True if FILE exists and has a size greater than zero.
[ -tFD ] True if file descriptor FD is open and refers to a terminal.
[ -uFILE ] True if FILE exists and its SUID (set user ID) bit is set.
[ -wFILE ] True if FILE exists and is writable.
[ -xFILE ] True if FILE exists and is executable.
[ -OFILE ] True if FILE exists and is owned by the effective user ID.
[ -GFILE ] True if FILE exists and is owned by the effective group ID.
[ -LFILE ] True if FILE exists and is a symbolic link.
[ -NFILE ] True if FILE exists and has been modified since it was last read.
[ -SFILE ] True if FILE exists and is a socket.
[ FILE1-ntFILE2 ] True if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not.
[ FILE1-otFILE2 ] True if FILE1 is older than FILE2, or is FILE2 exists and FILE1 does not.
[ FILE1-efFILE2 ] True if FILE1 and FILE2 refer to the same device and inode numbers.
[ -o OPTIONNAME ] True if shell option “OPTIONNAME” is enabled.
[ -z STRING ] True of the length if “STRING” is zero.
[ -n STRING ] or [ STRING ] True if the length of “STRING” is non-zero.
[ STRING1 == STRING2 ] True if the strings are equal. “=” may be used instead of “==” for strict POSIX compliance.
[ STRING1 != STRING2 ] True if the strings are not equal.
[ STRING1 < STRING2 ] True if “STRING1” sorts before “STRING2” lexicographically in the current locale.
[ STRING1 > STRING2 ] True if “STRING1” sorts after “STRING2” lexicographically in the current locale.
[ 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” are integers.

Expressions may be combined using the following operators, listed in decreasing order of precedence:

Combining expressions

Operation Effect
[ ! EXPR ] True if EXPR is false.
[ ( EXPR ) ] Returns the value of EXPR. This may be used to override the normal precedence of operators.
[ EXPR1 -a EXPR2 ] True if both EXPR1 and EXPR2 are true.
[ EXPR1 -o EXPR2 ] True if either EXPR1 or EXPR2 is true.

The [ (or test) built-in evaluates conditional expressions using a set of rules based on the number of arguments. More information about this subject can be found in the Bash documentation. Just like the if is closed with fi, the opening square bracket should be closed after the conditions have been listed.

Commands following the then statement

The CONSEQUENT-COMMANDS list that follows the then statement can be any valid UNIX command, any executable program, any executable shell script or any shell statement, with the exception of the closing fi. It is important to remember that the then and fi are considered to be separated statements in the shell. Therefore, when issued on the command line, they are separated by a semi-colon.

In a script, the different parts of the if statement are usually well-separated. Below, a couple of simple examples.


To check if file exists :

if [ -f /var/log/messages ]; then
echo "File /var/log/messages exist."

*You can use any combination that appropriate your needs.

Testing exit status

The ? variable holds the exit status of the previously executed command (the most recently completed foreground process).

The following example shows a simple test:

Th3-Gam3 ~ # if [ $? -eq 0 ]
> then echo "That was a good job!"
> fi
That was a good job!
Th3-Gam3 ~ #
Th3-Gam3 ~ # if ! grep myuser /etc/passwd; then echo "That user was not found"; fi
That user was not found
Th3-Gam3 ~ #

String comparisons

An example of comparing strings for testing the user ID:

Th3-Gam3 ~ # whoami 
Th3-Gam3 ~ # if [ "$(whoami)" != "user" ]
> then
> echo "You are not user"
> exit 1;
> fi
You are not user
akm@Th3-Gam3 ~ $

[] vs. [[]]

Contrary to [, [[ prevents word splitting of variable values. So, if VAR="var with spaces", you do not need to double quote $VAR in a test – eventhough using quotes remains a good habit. Also, [[ prevents pathname expansion, so literal strings with wildcards do not try to expand to filenames. Using [[, == and != interpret strings to the right as shell glob patterns to be matched against the value to the left, for instance: [[ "value" == val* ]] .

if/then/else/elif constructs

To test a condition and do something if true and another something if not true, use if/else.

To test multiple conditions , use if/elif, and you can use else too.

This is the full form of the if statement:








Th3-Gam3 ~ # if [ -f file.txt ]
> then
> echo "File found."
> elif [ -f ]
> then echo " exists"
> else 
> echo "Files NOT Found!"
> fi
Files NOT Found!

Nested if statements

Inside the if statement, you can use another if statement. You may use as many levels of nested ifs as you can logically manage.

And nested if can be shortened using the Boolean operators “AND” (&&) and “OR” (||).

*Use double brackets ” (( )) ” for testing an arithmetic expression or multiple conditions .

Using the exit statement and if

The exit statement takes an optional argument. This argument is the integer exit status code, which is passed back to the parent and stored in the $? variable.

A zero argument means that the script ran successfully. Any other value may be used by programmers to pass back different messages to the parent, so that different actions can be taken according to failure or success of the child process. If no argument is given to the exit command, the parent shell uses the current value of the $? variable.

Case statements

Nested if statements might be nice, but as soon as you are confronted with a couple of different possible actions to take, they tend to confuse. For the more complex conditionals, use the case syntax:


Each case is an expression matching a pattern. The commands in the COMMAND-LIST for the first match are executed. The “|” symbol is used for separating multiple patterns, and the “)” operator terminates a pattern list. Each case plus its according commands are called a clause. Each clause must be terminated with “;;”. Each case statement is ended with the esac statement.


echo "Enter number between 0 and 2 :"
read num

case $num in
echo "That was Zero";;
echo "That was One";;
echo "That was Tow";;
echo "Unknown Number for me!";;

Th3-Gam3 ~ # sh 
Enter number between 0 and 2 :
That was One
Th3-Gam3 ~ # sh 
Enter number between 0 and 2 :
Unknown Number for me!
Th3-Gam3 ~ # 


Now we learned how to build conditions into our scripts so that different actions can be undertaken upon success or failure of a command. The actions can be determined using the if statement. This allows you to perform arithmetic and string comparisons, and testing of exit code, input and files needed by the script.

A simple if/then/fi test often preceeds commands in a shell script in order to prevent output generation, so that the script can easily be run in the background or through the cron facility. More complex definitions of conditions are usually put in a case statement.

Upon successful condition testing, the script can explicitly inform the parent using the exit 0 status. Upon failure, any other number may be returned. Based on the return code, the parent program can take appropriate action.

That is it, i hope it was simple , thanks for joining me.
Enjoy !.


Comments are closed.