Bash If statement


Example

if [[ $1 -eq 1 ]]; then
    echo "1 was passed in the first parameter"
elif [[ $1 -gt 2 ]]; then
    echo "2 was not passed in the first parameter"
else
    echo "The first parameter was not 1 and is not more than 2."
fi

The closing fi is necessary, but the elif and/or the else clauses can be omitted.

The semicolons before then are standard syntax for combining two commands on a single line; they can be omitted only if then is moved to the next line.

It's important to understand that the brackets [[ are not part of the syntax, but are treated as a command; it is the exit code from this command that is being tested. Therefore, you must always include spaces around the brackets.

This also means that the result of any command can be tested. If the exit code from the command is a zero, the statement is considered true.

if grep "foo" bar.txt; then
    echo "foo was found"
else
    echo "foo was not found"
fi

Mathematical expressions, when placed inside double parentheses, also return 0 or 1 in the same way, and can also be tested:

if (( $1 + 5 > 91 )); then
    echo "$1 is greater than 86"
fi

You may also come across if statements with single brackets. These are defined in the POSIX standard and are guaranteed to work in all POSIX-compliant shells including Bash. The syntax is very similar to that in Bash:

if [ "$1" -eq 1 ]; then
    echo "1 was passed in the first parameter"
elif [ "$1" -gt 2 ]; then
    echo "2 was not passed in the first parameter"
else
    echo "The first parameter was not 1 and is not more than 2."
fi