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. |
if-else Statement
if-elif-else Statement
if Statement with Command Substitution
if Statement with Negated Condition
if Statement with Logical Operators
if Statement with File Tests
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 and is a block-special file
True if FILE exists and is a character-special file
True if FILE exists and is a directory
True if FILE exists and is a regular file
True if FILE exists and its SGID bit is set
True if FILE exists and is a symbolic link
True if FILE exists and its sticky bit is set
True if FILE exists and is a named pipe (FIFO)
True if FILE exists and is readable
True if FILE exists and has a size greater than zero
True if file descriptor FD is open and refers to a terminal
True if FILE exists and its SUID (set user ID) bit is set
True if FILE exists and is writable
True if FILE exists and is executable
True if FILE exists and is owned by the effective user ID
True if FILE exists and is owned by the effective group ID
True if FILE exists and is a symbolic link
True if FILE exists and has been modified since it was last read
True if FILE exists and is a socket
True if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not
True if FILE1 is older than FILE2, or if FILE2 exists and FILE1 does not
True if FILE1 and FILE2 refer to the same device and inode numbers
True if shell option "OPTIONNAME" is enabled
True if the length of "STRING" is zero
True if the length of "STRING" is non-zero
True if the strings are equal
True if the strings are not equal
True if "STRING1" sorts before "STRING2" lexicographically in the current locale
True if "STRING1" sorts after "STRING2" lexicographically in the current locale
OP is one of -eq, -ne, -lt, -le, -gt, or -ge
Combining Expressions
- Expressions can be combined using the following operators, listed in decreasing order of precedence
Returns the value of EXPR. This may be used to override the normal precedence of operators
True if both EXPR1 and EXPR2 are true
True if either EXPR1 or EXPR2 is true
Examples demonstrating the usage of primary expressions in if statements