Everything that can be useful in test constructs (if statements) in a bash environment.
This cheat sheet is based on the Advanced Bash-Scripting Guide by Mendel Cooper.
Compound Comparison
|
logical Similar to |
|
logical Similar to |
Integer Comparison
|
is equal to
|
|
is not equal to
|
|
is greater than
|
|
is greater than or equal to
|
|
is less than
|
|
is less than or equal to
|
|
is less than (within double parentheses)
|
|
is less than or equal to (within double parentheses)
|
|
is greater than (within double parentheses)
|
|
is greater than or equal to (within double parentheses)
|
String Comparison
|
is equal to The == comparison operator behaves differently within a double-brackets test than within single brackets. [[ $a == z* ]] # True if $a starts with an "z" (pattern matching). [[ $a == "z*" ]] # True if $a is equal to z* (literal matching). [ $a == z* ] # File globbing and word splitting take place. [ "$a" == "z*" ] # True if $a is equal to z* (literal matching). |
|
is not equal to if [ "$a" != "$b" ] This operator uses pattern matching within a [[ ... ]] construct. |
|
is less than, in ASCII alphabetical order Note that the if [[ "$a" < "$b" ]] if [ "$a" \< "$b" ] |
|
is greater than, in ASCII alphabetical order. Note that the if [[ "$a" > "$b" ]] if [ "$a" \> "$b" ] |
|
string is null that is, has zero length if [ -z "$s" ] |
|
string is not null. if [ -n "$s" ] |
File Test Operators
|
file exists
|
|
file is a regular file (not a directory or device file) |
|
file is a directory |
|
file is a symbolic link |
|
file is a block device |
|
file is a character device |
|
file is a pipe |
|
file is a socket |
|
file is not zero size |
|
file (descriptor) is associated with a terminal device This test option may be used to check whether the |
|
file has read permission (for the user running the test) |
|
file has write permission (for the user running the test) |
|
file has execute permission (for the user running the test) |
|
set-group-id (sgid) flag set on file or directory |
|
set-user-id (suid) flag set on file |
|
sticky bit set |
|
you are owner of file |
|
group-id of file same as yours |
|
file modified since it was last read |
|
file f1 is newer than f2
|
|
file f1 is older than f2
|
|
files f1 and f2 are hard links to the same file
|
|
"not" -- reverses the sense of the tests above (returns true if condition absent). |
Notes
Based on the Advanced Bash-Scripting Guide by Mendel Cooper.