Skip to content

Instantly share code, notes, and snippets.

@namp10010
Last active December 19, 2021 21:36
Show Gist options
  • Save namp10010/31045b436100a7ac1bc36d31db348301 to your computer and use it in GitHub Desktop.
Save namp10010/31045b436100a7ac1bc36d31db348301 to your computer and use it in GitHub Desktop.
Bash test operators copied from Kapeli page as I will always have to google it

Bash test operators

A gist copied from kapeli page

Integer Comparison

Integer Comparison
-eq is equal toif [ "$a" -eq "$b" ]
-ne is not equal toif [ "$a" -ne "$b" ]
-gt is greater thanif [ "$a" -gt "$b" ]
-ge is greater than or equal toif [ "$a" -ge "$b" ]
-lt is less thanif [ "$a" -lt "$b" ]
-le is less than or equal toif [ "$a" -le "$b" ]
< is less than(within double parentheses)(("$a" < "$b"))
<= is less than or equal to(within double parentheses)(("$a" <= "$b"))
> is greater than(within double parentheses)(("$a" > "$b"))
>= is greater than or equal to(within double parentheses)(("$a" >= "$b"))

String Comparison

String Comparison
=
==
is equal toThe == 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 toif [ "$a" != "$b" ]This operator uses pattern matching within a [[ ... ]] construct.
< is less than, in ASCII alphabetical orderNote that the < needs to be escaped within a [ ] construct.if [[ "$a" < "$b" ]] if [ "$a" \< "$b" ]
> is greater than, in ASCII alphabetical order.Note that the > needs to be escaped within a [ ] construct.if [[ "$a" > "$b" ]] if [ "$a" \> "$b" ]
-z string is null that is, has zero length if [ -z "$s" ]
-n string is not null.if [ -n "$s" ]

File Test Operators

File Test Operators
-e
-a
file exists-a is deprecated and its use is discouraged.
-f file is a regular file (not a directory or device file)
-d file is a directory
-h
-L
file is a symbolic link
-b file is a block device
-c file is a character device
-p file is a pipe
-S file is a socket
-s file is not zero size
-t file (descriptor) is associated with a terminal device
This test option may be used to check whether the stdin [ -t 0 ] or stdout [ -t 1 ] in a given script is a terminal.
-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
-nt file f1 is newer than f2if [ "$f1" -nt "$f2" ]
-ot file f1 is older than f2if [ "$f1" -ot "$f2" ]
-ef files f1 and f2 are hard links to the same fileif [ "$f1" -ef "$f2" ]
! "not" -- reverses the sense of the tests above (returns true if condition absent).

Compound Comparison

Compound Comparison
-a logical andSimilar to &&
-o logical orSimilar to `

References

  1. https://kapeli.com/cheat_sheets/Bash_Test_Operators.docset/Contents/Resources/Documents/index
  2. https://tldp.org/LDP/abs/html/tests.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment