
In bash, an if statement has the following structure.
if [ comparison ]; then
--do something--
elif [ comparison ]; then
--do something
else
--do something--
fi
For example.
if [ $foo -eq 1 ]; then
echo "Foo equals 1"
elif [ $foo -eq 2 ]; then
echo "Foo equals 2"
else
echo "Foo does not equal 1 or 2"
fi
The following table contains commonly used comparison operators.
Equals (integers) | if [ $foo -eq 1 ]; then |
Does not equal (integers) | if [ $foo -ne 1 ]; then |
Equals (strings) | if [ "$foo" == "Hello" ]; then |
Does not equal (strings) | if [ "$foo" != "Hello" ]; then |
Equals (dates) | if [ $today -eq $yesterday ]; then |
Greater than | if [ $foo -gt 1 ]; then |
Greater than or equal to | if [ $foo -ge 1 ]; then |
Less than | if [ $foo -lt 1 ]; then |
Less than or equal to | if [ $foo -le 1 ]; then |
Variable contains no value (null / empty) | if [ -z "$foo" ]; then |
Variable contains a value (defined / not null / not empty) | if [ ! -z "$foo" ]; then |
File exists | if [ -e "/path/to/file" ]; then |
File does not exist | if [ ! -e "/path/to/file" ]; then |
File is readable | if [ -r "/path/to/file" ]; then |
File is writable | if [ -w "/path/to/file" ]; then |
symlink exists | if [ -L "/path/to/file" ]; then |
symlink does not exist | if [ ! -L "/path/to/file" ]; then |
File is empty | if [ ! -s "/path/to/file" ]; then |
File is not empty | if [ -s "/path/to/file" ]; then |
File contains | if grep --quiet "Hello World" /path/to/file; then |
File does not contain | if ! grep --quiet "Hello World" /path/to/file; then |
Directory exists | if [ -d "/home/test" ]; then |
Directory does not exist | if [ ! -d "/home/test" ]; then |
Directory empty | if [ -z "$(ls -A /path/to/directory)" ]; then |
Directory not empty | if [ ! -z "$(ls -A /path/to/directory)" ]; then |
Variable contains | if [[ $foo = *"Hello World"* ]]; then |
Variable does not contain | if [[ $foo != *"Hello World"* ]]; then |
Variable begins with | if [[ $foo =~ ^"Hello" ]]; then |
Variable does not begin with | if [[ ! $foo =~ ^"Hello" ]]; then |
Variable ends with (do not place double quotes around value) | if [[ $foo =~ Hello$ ]]; then |
Variable does not end with (do not place double quotes around value) | if [[ ! $foo =~ Hello$ ]]; then |
List contains | if [[ "${list[@]}" =~ "$value" ]]; then |
List does not contain | if [[ ! "${list[@]}" =~ "$value" ]]; then |
Spacing
Many if statements requre a single white space inside of the brackets. I also use a single white space as my practice.
Case Insensitive
The following line can be included in a bash shell script to make all comparisons in the script case insensitive.
shopt -s nocasematch
And (&&), Or (||)
Double && characters can be used to link statements together as an and.
if [ $foo -eq 1 ] && [ $bar -eq 1 ]
if [[ $foo == "Hello" && $bar == "World" ]]
Double || characters can be used to link statements together as an or.
if [ $foo -eq 1 ] || [ $bar -eq 1 ]
if [[ $foo == "Hello" || $bar == "World" ]]
However, this might be a better syntax. The ^ and $ regular expression characters are used to ensure the $foo variable equals Hello or World (not contains).
if [[ $foo =~ ^(Hello|World)$ ]]; then
echo "foo equals Hello or World";
else
echo "foo does NOT equal Hello or World";
fi
Or the ^ and $ regular expression characters can be discarded to determine if the $foo variable contains Hello or World.
if [[ $foo =~ ^(Hello|World)$ ]]; then
echo "foo equals Hello or World";
else
echo "foo does NOT equal Hello or World";
fi
Do nothing (like Python pass)
There is kind of an odd and unique situation where sometimes, you do not want to do anything in your "then" or "else" clause. The : (colon) character can be used. The colon character is kind of like the Python pass statement, to simply do nothing and move on.
if [ $foo == "Hello" ]; then echo "foo equals Hello"; else :; fi
Did you find this article helpful?
If so, consider buying me a coffee over at