Bash Test Operators
Tuesday, January 22, 2008
Here’s a list I have compiled that explains many Bash test operators. This is pretty much all I use. I think there’s more, but I will add to it as I discover them. I’ve been wanting to put this online so I can use as a reference and I finally did. So here it is, enjoy!!
| FILES/DIRECTORIES | |
| -e | file exists |
| -f | file is a regular file (not a directory or device file) |
| -s (lowercase ’s’) | file is not zero size |
| -S | file is a socket |
| -d | file is a directory |
| -b | file is a block device (floppy, cdrom, etc.) |
| -c | file is a character device (keyboard, modem, sound card, etc.) |
| -p | file is a pipe |
| -h | file is a symbolic link |
| -L | file is a symbolic link |
| -t | file (descriptor) is associated with a terminal device |
| -r | file has read permission (for the user running the test) |
| -w | file has write permission (for the user running the test) |
| -x | file has execute permission (for the user running the test) |
| -g | set-group-id (sgid) flag set on file or directory |
| -u | set-user-id (suid) flag set on file |
| -k | sticky bit set |
| -O | you are owner of file |
| -G | group-id of file same as yours |
| -N | file modified since it was last read |
| f1 -nt f2 | file f1 is newer than f2 |
| f1 -ot f2 | file f1 is older than f2 |
| f1 -ef f2 | files f1 and f2 are hard links to the same file |
| INTEGERS | |
| -eq | is equal to |
| -ne | is not equal to |
| -gt | is greater than |
| -ge | is greater than or equal to |
| -lt | is less than |
| -le | 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) |
| STRINGS | |
| = | is equal to |
| == | 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 |
| < | is less than, in ASCII alphabetical order |
| > | is greater than, in ASCII alphabetical order |
| -n | string is not “null.” |
| -z | string is “null, ” that is, has zero length |
Done!!
