5 Examples for bash if else in shell script

Bash if (if-else-fi, if-elif-fi) helps to add logic to a shell script with conditions. Let us explore a few examples today.

The usage goes like this:

if -> Your-Conditional-expression -> then -> Condition-Met -> else -> Condition-Not-Met

if-then-else basic syntax:

if [conditional-expression]
then
  condition_met
else
  condition_NOT_met
fi

#1 if else in the shell script – basic if-then-else

Here is a simple practical example, which is running on the command line:

if [ $myvar ]
then
  echo "myvar is set"
else
  echo "myvar is NOT set"
fi

In this example, the variable $my var is not defined, and we used the if-then conditional statement to determine it.

#2 if else in the shell script – basic if-then-else with the condition is met.

We will slightly modify the first example scenario, here we will check for a variable that is set.

myvar=CodeTryout

if [ $myvar ]
then
  echo "myvar is set"
else
  echo "myvar is NOT set"
fi

When we run it, the if-else condition will be met, and the script prints the code block inside “then” section

#3 if-else in the shell script checking string match (false /match)

Next, let us check whether the variable is an exact match with a pre-defined string.

if [ $myvar = "CodeTryOut" ]
then
  echo "the variable myvar is set as CodeTryOut"
else
  echo "the variable myvar is NOT set as CodeTryOut"
fi

#4 if-else in the shell script checking string match (true / match)

With the above example, we will slightly modify the check by pre-defining the variable value, so we will get a true result.

myvar=CodeTryOut

if [ $myvar = "CodeTryOut" ]
then
  echo "the variable myvar is set as CodeTryOut"
else
  echo "the variable myvar is NOT set as CodeTryOut"
fi

#5 if-else-elif-fi in the shell script checking with multiple conditions and matches

if then statements can be chained further to check with different conditional expressions and take the corresponding action with it. Here is an example.

if [ $myvar = "CodeTryOut" ]
then
  echo "myvar is CodeTryOut"
elif [ $myvar = "codetryout" ]
then
  echo "myvar is codetryout"
else
  echo "my var is NOT CodeTryOut or codetryout"
fi

Runtime examples, for all three conditions. Note how the elif can be used to check the second expression.

  • Firstly, we don’t have a value set for myvar
  • It will print: my var is NOT CodeTryOut or codetryout
  • Secondly, we will set myvar=CodeTryOut
  • Then the script will print: myvar is CodeTryOut
  • Thirdly, we will set: myvar=codetryout
  • Then the script will print: myvar is codetryout

Demo:

[email protected] ~> if [ $myvar = "CodeTryOut" ]
> then
>   echo "myvar is CodeTryOut"
> elif [ $myvar = "codetryout" ]
> then
>   echo "myvar is codetryout"
> else
>   echo "my var is NOT CodeTryOut or codetryout"
> fi
my var is NOT CodeTryOut or codetryout
[email protected] ~>
[email protected] ~> myvar=CodeTryOut
[email protected] ~>
[email protected] ~> if [ $myvar = "CodeTryOut" ]
> then
>   echo "myvar is CodeTryOut"
> elif [ $myvar = "codetryout" ]
> then
>   echo "myvar is codetryout"
> else
>   echo "my var is NOT CodeTryOut or codetryout"
> fi
myvar is CodeTryOut
[email protected] ~>
[email protected] ~> myvar=codetryout
[email protected] ~>
[email protected] ~> if [ $myvar = "CodeTryOut" ]
> then
>   echo "myvar is CodeTryOut"
> elif [ $myvar = "codetryout" ]
> then
>   echo "myvar is codetryout"
> else
>   echo "my var is NOT CodeTryOut or codetryout"
> fi
myvar is codetryout

If you have more questions, please feel free to ask!