Introduction to Unix |
|
![]() |
![]() |
5.10. test¶
Linux for Programmers and Users, Sections 6.9.2 and 6.9.3
test
Evaluate a boolean expression setting the Exit Code to indicate a true or false result. This is used to express the logic of the Control Constructs used for shell script programming.
Note from the synopsis that there are two ways to invoke test – either with the command or the alternate form using square brackets. The square brackets have the advantage of giving a more familiar look, but one must be careful to leave spaces between the brackets and the boolean expression.
SYNOPSIS
test expression
[ expression ]
-n string | True if length of string is non-zero |
-z string | True if length of string is zero |
string1 == string2 | True if the strings are equal |
string1 != string2 | True if the strings are not equal |
Much of shell script programming often relates to working with files and directories, so the following boolean expressions are frequently used.
-a file | True if the file exists |
-b file | True if the file exists and is a block-oriented special file |
-c file | True if the file exists and is a character-oriented special file |
-d file | True if the file exists and is a directory |
-e file | True if the file exists |
-g file | True if the file exists and its “set group ID” bit is set |
-p file | True if the file exists and is a named pipe |
-r file | True if the file exists and is readable |
-s file | True if the file exists and has a size greater than zero |
-t fd | True if the file descriptor is open refers to the terminal |
-u file | True if the file exists and its “set user ID” bit is set |
-w file | True if the file exists and is writable |
-x file | True if the file exists and is executable |
-O file | True if the file exists and is owned the effective user ID of the user. |
-G file | True if the file exists and is owned the effective group ID of the user. |
-L file | True if the file exists and is a symbolic link |
-N file | True if the file exists and has been modified since it was last read |
-S file | True if the file exists and is a named socket |
file1 -nt file2 | True if file1 is newer than file2 |
file1 -ot file2 | True if file1 is older than file2 |
file1 -ef file2 | True if file1 and file2 have the same device and inode numbers |
See also