Show Menu
Cheatography

Bash_test_expressions Cheat Sheet by

Test command expressions

File Expres­sions

file1 -ef file2
file1 and file2 have the same inode numbers (the two filenames refer to the same file by hard linking).
file1 -nt file2
file1 is newer than file2.
file1 -ot file2
file1 is older than file2.
-b file
file exists and is a block-­special (device) file.
-c file
file exists and is a charac­ter­-sp­ecial (device) file.
-d file
file exists and is a directory.
-e file
file exists.
-f file
file exists and is a regular file.
-g file
file exists and is set-gr­oup-ID.
-G file
file exists and is owned by the effective group ID.
-k file
file exists and has its "­sticky bit" set.
-L file
file exists and is a symbolic link.
-O file
file exists and is owned by the effective user ID.
-p file
file exists and is a named pipe.
-r file
file exists and is readable (has readable permission for the effective user).
-s file
file exists and has a length greater than zero.
-S file
file exists and is a network socket.
-t fd
fd is a file descriptor directed to/from the terminal. This can be used to determine wheter standard input/­out­put­/error is being redire­cted.
-u file
file exists and is setuid.
-w file
file exists and is writable (has write permission for the effective user).
-x file
file exists and is executable (has execut­e/s­earch permission for the effective user).
 

Logical Expres­sions

AND
-a
OR
-o
NOT
!

Integer Expres­sions

integer1 -eq integer2
integer1 is equal to integer2.
integer1 -ne integer2
integer1 is not equal to integer2.
integer1 -le integer2
integer1 is less than or equal to integer2.
integer1 -lt integer2
integer1 is less than integer2.
integer1 -ge integer2
integer1 is greater than or equal to integer2.
integer1 -gt integer2
integer1 is greater than integer2.

String Expres­sions

string
string is not null.
-n string
The length of string is greater than zero.
-z string
The length of string is zero.
string1 = string2
string1 == string2
string1 and string2 are equal.
string1 != string2
string1 and string2 are not equal.
string1 > string2
string1 sorts after string2.
string1 < string2
string1 sorts before string2.

Modern Version of test

Instead of using [ expression ] is going to be [[ expression ]] and one designed for integers is (( expression )).

With [[ expression ]] we can have an expression matching a regular expres­sion:
string =~ regex.

For the integers in this way we can use the conven­tional way of other progra­mming languages expres­sions like:
int == 0.
int < 0.
int > 0.
int % 0.

And of course the logical operators, instead of using the older options of test we can use:
&& for and.
|| for or.
! for not.
           
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Linux Command Line Cheat Sheet
          bash Shortcuts Cheat Sheet
          Bash Script Colors Cheat Sheet