Bootstrap FreeKB - Bash (Scripting) - if, elif, else, then statements
Bash (Scripting) - if, elif, else, then statements

Updated:   |  Bash (Scripting) articles

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 thanif [ $foo -gt 1 ]; then
Greater than or equal to if [ $foo -ge 1 ]; then
Less thanif [ $foo -lt 1 ]; then
Less than or equal toif [ $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 existsif [ -e "/path/to/file" ]; then
File does not existif [ ! -e "/path/to/file" ]; then
File is readableif [ -r "/path/to/file" ]; then
File is writableif [ -w "/path/to/file" ]; then
symlink existsif [ -L "/path/to/file" ]; then
symlink does not existif [ ! -L "/path/to/file" ]; then
File is emptyif [ ! -s "/path/to/file" ]; then
File is not emptyif [ -s "/path/to/file" ]; then
File containsif grep --quiet "Hello World" /path/to/file; then
File does not containif ! grep --quiet "Hello World" /path/to/file; then
Directory existsif [ -d "/home/test" ]; then
Directory does not existif [ ! -d "/home/test" ]; then
Directory emptyif [ -z "$(ls -A /path/to/directory)" ]; then
Directory not emptyif [ ! -z "$(ls -A /path/to/directory)" ]; then
Variable containsif [[ $foo = *"Hello World"* ]]; then
Variable does not containif [[ $foo != *"Hello World"* ]]; then
Variable begins withif [[ $foo =~ ^"Hello" ]]; then
Variable does not begin withif [[ ! $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 containsif [[ "${list[@]}" =~ "$value" ]]; then
List does not containif [[ ! "${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 Buy Me A Coffee



Comments


Add a Comment


Please enter 8614d3 in the box below so that we can be sure you are a human.