Skip to content

Instantly share code, notes, and snippets.

@deanrather
Last active March 20, 2023 02:21
Show Gist options
  • Select an option

  • Save deanrather/b52c7166a8de31877bc6 to your computer and use it in GitHub Desktop.

Select an option

Save deanrather/b52c7166a8de31877bc6 to your computer and use it in GitHub Desktop.
Bash Comparison Operators

Bash Comparison Operators

Integer Comparison

Operator   Description                      Example
-eq        Is Equal To                      [ 100 -eq 100 ]
-ne        Is Not Equal To                  [ 100 -ne 200 ]
-gt        Is Greater Than                  [ 200 -gt 100 ]
-ge        Is Greater Than Or Equal To      [ 100 -ge 100 ]
-lt        Is Less Than                     [ 100 -lt 200 ]
-le        Is Less Than Or Equal To         [ 100 -le 100 ]
==         Is Equal To                      (( 100 == 100 ))
!=         Is Not Equal To                  (( 100 != 200 ))
<          Is Less Than                     (( 100 < 200 ))
<=         Is Less Than Or Equal To         (( 100 <= 100 ))
>          Is Greater Than                  (( 200 > 100 ))
>=         Is Greater Than Or Equal To      (( 200 >= 100 ))

String Comparison

Operator   Description Example
= or ==    Is Equal To                      [ abc == abc ]
!=         Is Not Equal To                  [ abc != def ]
>          Is Greater Than (ASCII)          [ def > abc ]
>=         Is Greater Than Or Equal To      [ def >= def ]
<          Is Less Than                     [ abc < def ]
<=         Is Less Than Or Equal To         [ abc <= def ]
-n         Is Not Null                      [ -n abc ]
-z         Is Null (Zero Length String)     [ -z "" ]

File Tests

Operator   Description                      Example
-d         Directory                        [ -d /tmp/abc ]
-e         Exists (also -a)                 [ -e /tmp/abc ]
-f         Regular file                     [ -f /tmp/abc ]
-h         Symbolic link (also -L)          [ -h /tmp/abc ]
-p         Named pipe                       [ -p /tmp/abc ]
-r         Readable by you                  [ -r /tmp/abc ]
-s         Not empty                        [ -s /tmp/abc ]
-S         Socket                           [ -S /tmp/abc ]
-w         Writable by you                  [ -w /tmp/abc ]
-N         Modified since last read         [ -N /tmp/abc ]

File Comparison

Operator   Description                      Example
-nt        Newer Than                       [ /tmp/a -nt /tmp/b ]
-ot        Older Than                       [ /tmp/a -ot /tmp/b ]
-ef        Hard Link To                     [ /tmp/a -ef /tmp/b ]

Sources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment