Bash conditional operators guide #bash #conditionaloperators
This document provides an overview of common bash conditional operators (unary test options) used in bash within [[ ... ]]
or [ ... ]
expressions.
String Tests
Operator | Description | Example |
---|
-z | Tests if the string is empty (zero length). | [[ -z "$var" ]] – True if $var is empty. |
-n | Tests if the string is non-empty. | [[ -n "$var" ]] – True if $var is non-empty. |
== | String comparison for equality. Bash-specific. | [[ "$var1" == "$var2" ]] – True if $var1 equals $var2 . |
!= | String comparison for inequality. | [[ "$var1" != "$var2" ]] – True if $var1 is not equal to $var2 . |
File Tests
Operator | Description | Example |
---|
-e | Checks if the file or directory exists. | [[ -e /path/to/file ]] – True if the file exists. |
-f | Checks if the path is a regular file. | [[ -f /path/to/file ]] – True if it is a regular file. |
-d | Checks if the path is a directory. | [[ -d /path/to/dir ]] – True if it is a directory. |
-r | Checks if the file is readable. | [[ -r /path/to/file ]] – True if the file is readable. |
-w | Checks if the file is writable. | [[ -w /path/to/file ]] – True if the file is writable. |
-x | Checks if the file is executable. | [[ -x /path/to/file ]] – True if the file is executable. |
-s | Checks if the file has size greater than zero. | [[ -s /path/to/file ]] – True if the file has non-zero size. |
-L | Checks if the path is a symbolic link. | [[ -L /path/to/link ]] – True if it is a symlink. |
-p | Checks if the file is a named pipe (FIFO). | [[ -p /path/to/pipe ]] – True if it is a named pipe. |
-S | Checks if the file is a socket. | [[ -S /path/to/socket ]] – True if it is a socket. |
-b | Checks if the file is a block device. | [[ -b /path/to/device ]] – True if it is a block device. |
-c | Checks if the file is a character device. | [[ -c /path/to/device ]] – True if it is a character device. |
Permissions and Ownership Tests
Operator | Description | Example |
---|
-u | Checks if the file has the setuid bit set. | [[ -u /path/to/file ]] – True if setuid is set. |
-g | Checks if the file has the setgid bit set. | [[ -g /path/to/file ]] – True if setgid is set. |
-k | Checks if the file has the sticky bit set. | [[ -k /path/to/file ]] – True if the sticky bit is set. |
-O | Checks if the current user owns the file. | [[ -O /path/to/file ]] – True if owned by the current user. |
-G | Checks if the file’s group matches the user’s group. | [[ -G /path/to/file ]] – True if group matches. |
Integer Comparisons
Operator | Description | Example |
---|
-eq | Tests if two integers are equal. | [[ "$num1" -eq "$num2" ]] – True if $num1 equals $num2 . |
-ne | Tests if two integers are not equal. | [[ "$num1" -ne "$num2" ]] – True if $num1 is not equal to $num2 . |
-lt | Tests if one integer is less than another. | [[ "$num1" -lt "$num2" ]] – True if $num1 is less than $num2 . |
-le | Tests if one integer is less than or equal to another. | [[ "$num1" -le "$num2" ]] – True if $num1 is less than or equal to $num2 . |
-gt | Tests if one integer is greater than another. | [[ "$num1" -gt "$num2" ]] – True if $num1 is greater than $num2 . |
-ge | Tests if one integer is greater than or equal to another. | [[ "$num1" -ge "$num2" ]] – True if $num1 is greater than or equal to $num2 . |
Miscellaneous
Operator | Description | Example |
---|
-t | Tests if the file descriptor is open and refers to a terminal. | [[ -t 1 ]] – True if file descriptor 1 is a terminal. |
-v | Tests if a variable is set (bash-specific). | [[ -v varname ]] – True if varname is set. |
-R | Tests if a file is readable by the real user (instead of the effective user). | [[ -R /path/to/file ]] – True if readable by real user. |
Combining Conditions
You can combine multiple conditions using logical operators:
Operator | Description | Example |
---|
&& | Logical AND. | [[ -n "$var" && -f "$file" ]] – True if $var is non-empty AND $file exists. |
` | | ` |
! | Logical NOT. | [[ ! -f "$file" ]] – True if $file does NOT exist. |