test command.
/usr/bin/test
test # bash version.
see CONDITIONAL EXPRESSIONS in bash man page
[ ] - the left and right square brackets.
Note that bash has an internal form of test that may vary somewhat from the
stand alone version of test.
test tests something based on option choice and arguments.
And sets the status code, $?.
Commonly used with conditional portion shell script commands such as :
if, while, or until statements.
Common tests
* Generally, it is a good practice to quote variable in case they are null
or undefined.
Or if variable contents has white-space or non alpha-numeric characters
such as ;
String tests
-n STRING - tests for non-zero length of string.
test -n "$response"
-z STRING - tests if string is zero length.
STRING1 = STRING2 - strings match for equality.
Usually, at lease one of these is a variable.
test "$response" = yes
test yes = $response"
var="00"; test "$var" == 0 # This is not a match
# bash version of test supports both = and ==
# but the single = is POSIX compliant.
STRING1 != STRING2 - strings don't match for equality.
Integer tests
INTEGER1 -eq INTEGER2 - numeric match for equality.
Usually, at lease one of these is a variable.
In this case, 0 does equal 00.
Other integer tests.
-ge - is left integer greater than or equal to right argument.
-gt - is left integer greater than right argument.
-le - is left integer less than or equal to right argument.
-lt - is left integer less than right argument.
-ne - is left integer not equal to right argument.
File tests
-a FILE - does file exist, any kind. (bash)
-b FILE - does file exist and is it a block special file?
-c FILE - does file exist and is it a character special file?
-d FILE - does file exist and is it a directory file?
-e FILE - does file exist?
-f FILE - does file exist and is it a regular file?
-h FILE - does file exist and is it a symbolic link file?
-L FILE - does file exist and is it a symbolic link file?
-p FILE - does file exist and is it a named pipe file?
-r FILE - does file exist and readable?
-w FILE - does file exist and writable?
-x FILE - does file exist and executable?
There are other tests dealing with SUID, SGID, sticky bit, etc.
Except for -h and -L, test will follow a symbolic link to actual file.
File comparisons
FILE1 -ef FILE2 - files have same device and inode (same file).
FILE1 -nt FILE2 - FILE1 is newer than FILE2.
FILE1 -ot FILE2 - FILE1 is older than FILE2.
Compound tests
EXPR1 -a EXPR2 - both expressions test true,
where EXPR1 and EXP2 are any two of the tests listed above.
EXPR1 -o EXPR2 - either expression tests true,
where EXPR1 and EXP2 are any two of the tests listed above.
See CONDITIONAL EXPRESSIONS section of the bash man page for additional
test conditions available for the bash version of test.