Table of Contents

Bash - If then else

About

if execute conditionally a block with a list of command based on the exit status of a list of command

You can also use control operators to run conditionally a command

Syntax

if list; then 
list; 
[ elif list; then list; ]... 
[ else list; ] 
fi

Nested if example

if lists;
then
  ...
else
  if lists;
  then 
     # code if 'expression' is true.
  fi
fi 

Exit Status

The exit status of the entire construct is:

Example

With a conditional expression command

if [ "foo" = "foo" ]; then
   echo expression evaluated as true
else
    echo expression evaluated as false
fi

Documentation / Reference

help if