Skip to content

Instantly share code, notes, and snippets.

@bynect
Last active September 4, 2020 19:42
Show Gist options
  • Save bynect/c0e61e4e82b9c53c27abd3aa4489e077 to your computer and use it in GitHub Desktop.
Save bynect/c0e61e4e82b9c53c27abd3aa4489e077 to your computer and use it in GitHub Desktop.

String operator

Operator Description Example
= is equal to [ $a = $b ]
== is equal to [ $a == $b ]
!= is not equal to [ $a != $b ]
> is greater than, in alphabetical order [ $a > $b ]
< is less than, in alphabetical order [ $a < $b ]
-z has zero length [ -z $a ]
-n has non-zero length [ -n $a ]
str is not an empty string [ $a ]

Integer operator

Operator Description Example
-eq is equal to [ $a -eq $b ]
-ne is not equal to [ $a -ne $b ]
-gt is greater than [ $a -gt $b ]
-ge is greater than or equal to [ $a -ge $b ]
-lt is less than [ $a -lt $b ]
-le is less than or equal to [ $a -le $b ]
> is greater than (( $a > $b ))
=> is greater than or equal to (( $a >= $b ))
< is less than (( $a < $b ))
<= is less than or equal to (( $a <= $b ))

Boolean operator

Operator Description Example
! logic NOT [ ! $a ]
-o logic OR [ $a -lt 20 -o $b -gt 100 ]
-a logic AND [ $a -lt 20 -a $b -gt 100 ]

File test operator

Operator Description Example
-b file checks if file is a block special file [ -b file ]
-c file checks if file is a character special file [ -c file ]
-d file checks if file is a directory [ -d file ]
-f file checks if file is an ordinary file [ -f file ]
-g file checks if file has its set group ID (SGID) bit set [ -g file ]
-k file checks if file has its sticky bit set [ -k file ]
-p file checks if file is a named pipe [ -p file ]
-t file checks if file descriptor is open and associated with a terminal [ -t file ]
-u file checks if file has its Set User ID (SUID) bit set [ -u file ]
-r file checks if file is readable [ -r file ]
-w file checks if file is writable [ -w file ]
-x file checks if file is executable [ -x file ]
-s file checks if file has size greater than 0 [ -s file ]
-e file checks if file exists [ -e file ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment