In this tutorial, you will learn how to use Bash IF Else statements in your shell commands.
The IF logical operator is commonly used in programming languages to control flow. You use it to check the state of a value, whether it is equal to another or not; or whether it is set or not, for example.
We’ll first look at the basic
if statement is Bash, and then move onto
IF Bash Statement
The basic syntax of an IF statement in bash looks like the following. If you have programming experience, you’ll notice it’s fairly different from what you are use to.
if TEST-COMMAND then STATEMENTS fi
TEST-COMMAND is where you write your test case to check if something is true or not.
- If your tests result is true, then a statement or number of statements will
- If your test result is false, then no statement defined in your IF statement will be executed.
IF..Else Bash Statement
When writing an IF statement execute statements whether the test results true or false, you use the
if TEST-COMMAND then STATEMENTS else STATEMENTS fi
By using the
else operator, your
if statement will execute a different set of statements depending on your test case.
- If a test results in true, execute statements after
- If a test results in false, execute statements after the
IF ELIF Bash Statement
Multiple IF tests can be used within a single IF statement block. To do this with BASH or Shell Script the
elif operator is used.
if TEST-COMMAND then STATEMENTS elif TEST-COMMAND STATEMENTS else STATEMENTS fi
In the example above, we’ve combined an
if statement with an
elif and and an
- If the first test is true, execute statements immediately following.
- If the first test is false and the second test case is true, execute statements immediately after the
- If the first two test cases return false execute statements immediately following the
When you are performing your comparison tests you use an operator. Operators are provided for integer comparisons, string comparisons, and file comparisons.
Logical Operators for Integer Comparison
|Greater than or equal to|
|Less than or equal to|
|Not equal to|
Logical Operators for String Comparison
|==||is equal to|
|is not equal to|
|String is null|
|String is not null|
|ASCII value less than|
|ASCII value greater than|
Logical Operators for Files
|file is a regular file, /|
not a directory
|File is a directory|
|File is not zero size|
|File is symbolic link|
|File is a block device|
|File is a pipe device|
|File is a socket|
|File has read permission for user.|
|File has write permission for user.|
Bash Integer Comparisons
If Greater Than or Else
To check if one value or variable is greater than a value you use the
-gt flag in your test.
[[ x -gt y ]]
Used in an example, the following if logical checks whether the variable
$foo is greater than
if [[ $foo -gt 10 ]] then echo $foo is greater than 10 else echo $foo is not greater then 10 fi
If Less Than or Else
if [[ $foo -lt 10 ]] then echo $foo is greater than 10 else echo $foo is not greater then 10 fi
If Equal To or Else
if [[ $foo -eq 10 ]] then echo $foo is greater than 10 else echo $foo is not greater then 10 fi
Bash String Comparisons
If Variable is Null
To check if a variable is null (not set) use the
-z string comparison operators. In the following example, we check to verify that the
$foo variable is set.
if [[ -z $foo ]] then echo "$foo is not set" fi
If Variable is not Null
To check if a variable is not null use the
-n string comparison operator. For example, to check if the variable
$bar is not null, you would write your
if statement as follows.
if [[ -n $foo ]] then echo "$foo is not set" fi
If Variable is Equal To String
To check whether two strings are equal or match each other, you use the
== string comparison operator.
For example, to check whether
$var1 is equal to
"cats" you would write the following
if [[ $var1 == "cats" ]] then echo "$var1 equals 'cats'" fi