Skip to content

Bash if Conditional Expressions: Complete Guide with Examples

Unlock the full potential of if conditional expressions in Bash scripts with this comprehensive guide. Explore primary and combined expressions for checking file attributes, performing string comparisons, evaluating arithmetic expressions, and conducting logical operations. Whether you're validating file existence, comparing strings, or executing code based on numerical conditions, this guide provides in-depth examples and explanations to enhance your Bash scripting skills.


Expressions used with if

Primary Meaning
[ -a FILE ] True if FILE exists.
[ -b FILE ] True if FILE exists and is a block-special file.
[ -c FILE ] True if FILE exists and is a character-special file.
[ -d FILE ] True if FILE exists and is a directory.
[ -e FILE ] True if FILE exists.
[ -f FILE ] True if FILE exists and is a regular file.
[ -g FILE ] True if FILE exists and its SGID bit is set.
[ -h FILE ] True if FILE exists and is a symbolic link.
[ -k FILE ] True if FILE exists and its sticky bit is set.
[ -p FILE ] True if FILE exists and is a named pipe (FIFO).
[ -r FILE ] True if FILE exists and is readable.
[ -s FILE ] True if FILE exists and has a size greater than zero.
[ -t FD ] True if file descriptor FD is open and refers to a terminal.
[ -u FILE ] True if FILE exists and its SUID (set user ID) bit is set.
[ -w FILE ] True if FILE exists and is writable.
[ -x FILE ] True if FILE exists and is executable.
[ -O FILE ] True if FILE exists and is owned by the effective user ID.
[ -G FILE ] True if FILE exists and is owned by the effective group ID.
[ -L FILE ] True if FILE exists and is a symbolic link.
[ -N FILE ] True if FILE exists and has been modified since it was last read.
[ -S FILE ] True if FILE exists and is a socket.
[ FILE1 -nt FILE2 ] True if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not.
[ FILE1 -ot FILE2 ] True if FILE1 is older than FILE2, or if FILE2 exists and FILE1 does not.
[ FILE1 -ef FILE2 ] True if FILE1 and FILE2 refer to the same device and inode numbers.
[ -o OPTIONNAME ] True if shell option "OPTIONNAME" is enabled.
[ -z STRING ] True if the length of "STRING" is zero.
[ -n STRING ] or [ STRING ] True if the length of "STRING" is non-zero.
[ STRING1 == STRING2 ] True if the strings are equal.
[ STRING1 != STRING2 ] True if the strings are not equal.
[ STRING1 < STRING2 ] True if "STRING1" sorts before "STRING2" lexicographically in the current locale.
[ STRING1 > STRING2 ] True if "STRING1" sorts after "STRING2" lexicographically in the current locale.
[ ARG1 OP ARG2 ] "OP" is one of -eq, -ne, -lt, -le, -gt or -ge.

Combining expressions

Operation Effect
[ ! EXPR ] True if EXPR is false.
[ ( EXPR ) ] Returns the value of EXPR. This may be used to override the normal precedence of operators.
[ EXPR1 -a EXPR2 ] True if both EXPR1 and EXPR2 are true.
[ EXPR1 -o EXPR2 ] True if either EXPR1 or EXPR2 is true.

Check if it's root running

if [ "$EUID" -ne 0 ]; then
    echo "Please run this as root"
    exit 1
fi

Check if it´s root running

if [ $(id -u) -ne 0 ];then
    echo "You are not root!"
    exit;
fi

True if FILE exists

if [ -a FILE ] then
    # Code block to execute if the condition is true
fi

if-else Statement

if condition; then
    # Code block to execute if the condition is true
else
    # Code block to execute if the condition is false
fi

if-elif-else Statement

if condition1; then
    # Code block to execute if condition1 is true
elif condition2; then
    # Code block to execute if condition2 is true
else
    # Code block to execute if both condition1 and condition2 are false
fi

if Statement with Command Substitution

if $(command); then
    # Code block to execute if the command is successful
fi

if Statement with Negated Condition

if ! condition; then
    # Code block to execute if the condition is false
fi

if Statement with Logical Operators

if [[ condition1 && condition2 ]]; then
    # Code block to execute if both condition1 and condition2 are true
fi

if [[ condition1 || condition2 ]]; then
    # Code block to execute if either condition1 or condition2 is true
fi

if Statement with File Tests

if [[ -e file ]]; then
    # Code block to execute if the file exists
fi

if [[ -d directory ]]; then
    # Code block to execute if the directory exists
fi

if [[ -f file && -r file && -s file ]]; then
    # Code block to execute if the file is readable and has a non-zero size
fi

if Statement with String Comparisons

if [[ "$var1" == "$var2" ]]; then
    # Code block to execute if var1 and var2 are equal
fi

if [[ "$var1" != "$var2" ]]; then
    # Code block to execute if var1 and var2 are not equal
fi

if [[ -z "$var" ]]; then
    # Code block to execute if var is an empty string
fi

if [[ -n "$var" ]]; then
    # Code block to execute if var is a non-empty string
fi

if Statement with Arithmetic Comparisons

if (( num1 > num2 )); then
    # Code block to execute if num1 is greater than num2
fi

if (( num1 >= num2 )); then
    # Code block to execute if num1 is greater than or equal to num2
fi

if (( num1 < num2 )); then
    # Code block to execute if num1 is less than num2
fi

if (( num1 <= num2 )); then
    # Code block to execute if num1 is less than or equal to num2
fi

if (( num1 == num2 )); then
    # Code block to execute if num1 is equal to num2
fi

if (( num1 != num2 )); then
    # Code block to execute if

 num1 is not equal to num2
fi

Primary Expressions

True if FILE exists

if [ -a FILE ]; then
    # Code block to execute if the condition is true
fi

True if FILE exists and is a block-special file

if [ -b FILE ]; then
    # Code block to execute if the condition is true
fi

True if FILE exists and is a character-special file

if [ -c FILE ]; then
    # Code block to execute if the condition is true
fi

True if FILE exists and is a directory

if [ -d FILE ]; then
    # Code block to execute if the condition is true
fi

True if FILE exists

if [ -e FILE ]; then
    # Code block to execute if the condition is true
fi

True if FILE exists and is a regular file

if [ -f FILE ]; then
    # Code block to execute if the condition is true
fi

True if FILE exists and its SGID bit is set

if [ -g FILE ]; then
    # Code block to execute if the condition is true
fi

True if FILE exists and is a symbolic link

if [ -h FILE ]; then
    # Code block to execute if the condition is true
fi

True if FILE exists and its sticky bit is set

if [ -k FILE ]; then
    # Code block to execute if the condition is true
fi

True if FILE exists and is a named pipe (FIFO)

if [ -p FILE ]; then
    # Code block to execute if the condition is true
fi

True if FILE exists and is readable

if [ -r FILE ]; then
    # Code block to execute if the condition is true
fi

True if FILE exists and has a size greater than zero

if [ -s FILE ]; then
    # Code block to execute if the condition is true
fi

True if file descriptor FD is open and refers to a terminal

if [ -t FD ]; then
    # Code block to execute if the condition is true
fi

True if FILE exists and its SUID (set user ID) bit is set

if [ -u FILE ]; then
    # Code block to execute if the condition is true
fi

True if FILE exists and is writable

if [ -w FILE ]; then
    # Code block to execute if the condition is true
fi

True if FILE exists and is executable

if [ -x FILE ]; then
    # Code block to execute if the condition is true
fi

True if FILE exists and is owned by the effective user ID

if [ -O FILE ]; then
    # Code block to execute if the condition is true
fi

True if FILE exists and is owned by the effective group ID

if [ -G FILE ]; then
    # Code block to execute if the condition is true
fi

True if FILE exists and is a symbolic link

if [ -L FILE ]; then
    # Code block to execute if the condition is true
fi

True if FILE exists and has been modified since it was last read

if [ -N FILE ]; then
    # Code block to execute if the condition is true
fi

True if FILE exists and is a socket

if [ -S FILE ]; then
    # Code block to execute if the condition is true
fi

True if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not

if [ FILE1 -nt FILE2 ]; then
    # Code block to execute if the condition is true
fi

True if FILE1 is older than FILE2, or if FILE2 exists and FILE1 does not

if [ FILE1 -ot FILE2 ]; then
    # Code block to execute if the condition is true
fi

True if FILE1 and FILE2 refer to the same device and inode numbers

if [ FILE1 -ef FILE2 ]; then
    # Code block to execute if the condition is true
fi

True if shell option "OPTIONNAME" is enabled

if [ -o OPTIONNAME ]; then
    # Code block to execute if the condition is true
fi

True if the length of "STRING" is zero

if [ -z STRING ]; then
    # Code block to execute if the condition is true
fi

True if the length of "STRING" is non-zero

if [ -n STRING ] || [ STRING ]; then
    # Code block to execute if the condition is true
fi

True if the strings are equal

if [ STRING1 == STRING2 ]; then
    # Code block to execute if the condition is true
fi

True if the strings are not equal

if [ STRING1 != STRING2 ]; then
    # Code block to execute if the condition is true
fi

True if "STRING1" sorts before "STRING2" lexicographically in the current locale

if [ STRING1 < STRING2 ]; then
    # Code block to execute if the condition is true
fi

True if "STRING1" sorts after "STRING2" lexicographically in the current locale

if [ STRING1 > STRING2 ]; then
    # Code block to execute if the condition is true
fi

OP is one of -eq, -ne, -lt, -le, -gt, or -ge

if [ ARG1 OP ARG2 ]; then
    # Code block to execute if the condition is true
fi

Combining Expressions

  • Expressions can be combined using the following operators, listed in decreasing order of precedence

True if EXPR is false

if [ ! EXPR ]; then
    # Code block to execute if the condition is true
fi

Returns the value of EXPR. This may be used to override the normal precedence of operators

if [ ( EXPR ) ]; then
    # Code block to execute if the condition is true
fi

True if both EXPR1 and EXPR2 are true

if [ EXPR1 -a EXPR2 ]; then
    # Code block to execute if the condition is true
fi

True if either EXPR1 or EXPR2 is true

if [ EXPR1 -o EXPR2 ]; then
    # Code block to execute if the condition is true
fi

Examples demonstrating the usage of primary expressions in if statements

if [ -a FILE ]; then
    # Code block to execute if the condition is true
fi
if [ -b FILE ]; then
    # Code block to execute if the condition is true
fi
if [ -c FILE ]; then
    # Code block to execute if the condition is true
fi
if [ -d FILE ]; then
    # Code block to execute if the condition is true
fi
if [ -e FILE ]; then
    # Code block to execute if the condition is true
fi
if [ -f FILE ]; then
    # Code block to execute if the condition is true
fi
if [ -g FILE ]; then
    # Code block to execute if the condition is true
fi
if [ -h FILE ]; then
    # Code block to execute if the condition is true
fi
if [ -k FILE ]; then
    # Code block to execute if the condition is true
fi
if [ -p FILE ]; then
    # Code block to execute if the condition is true
fi