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
- The if list. The command list) is executed (Generally there is one or more conditional command expression in the list of command)
- If its exit status is:
- zero, then the then list list is executed.
- Otherwise, each elif list list is executed in turn, and if its exit status is zero:
- the corresponding then list list is executed and the if command completes.
- Otherwise, the else list list is executed, if present.
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:
- the exit status of the last command executed,
- or zero if no condition tested true.
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