Bash Conditionals

If statements and conditions

If Statement

if [ condition ]; then
  commands
fi

If-Else

if [ condition ]; then
  commands
else
  commands
fi

If-Elif-Else

if [ condition1 ]; then
  commands
elif [ condition2 ]; then
  commands
else
  commands
fi

String Comparison

[ $a = $b ] # equal
[ $a != $b ] # not equal
[ -z $a ] # string is empty
[ -n $a ] # string is not empty

Numeric Comparison

[ $a -eq $b ] # equal
[ $a -ne $b ] # not equal
[ $a -gt $b ] # greater than
[ $a -ge $b ] # greater or equal
[ $a -lt $b ] # less than
[ $a -le $b ] # less or equal

File Tests

[ -e file ] # file exists
[ -f file ] # is regular file
[ -d file ] # is directory
[ -r file ] # is readable
[ -w file ] # is writable
[ -x file ] # is executable
[ -s file ] # file size > 0

Logical Operators

[ condition1 ] && [ condition2 ] # AND
[ condition1 ] || [ condition2 ] # OR
[ ! condition ] # NOT