Linux Test Command Information and Examples
Linux Test Command Information and Examples
About test
Checks file types and compares values.
Description
test is used as part of the conditional execution of shell commands.
test exits with the status determined by EXPRESSION. Placing the EXPRESSION between
square brackets ([ and ]) is the same as testing the EXPRESSION with test. To see the exit
status at the command prompt, echo the value "$?" A value of 0 means the expression
evaluated as true, and a value of 1 means the expression evaluated as false.
test syntax
test EXPRESSION
[ EXPRESSION ]
Expressions
Expressions take the following forms:
FILE1 -ef FILE2 FILE1 and FILE2 have the same device and inode
numbers
Except for -h and -L, all FILE-related tests dereference symbolic links. Beware that
parentheses need to be escaped (e.g., by backslashes) for shells. INTEGER may also be -l
STRING, which evaluates to the length of STRING.
NOTE: your shell may have its own version of test, which usually supersedes the version
described here. Please refer to your shell's documentation for details about the options it
supports.
test examples
test 100 -gt 99 && echo "Yes, that's true." || echo "No, that's
false."
This command will print the text "Yes, that's true." because 100 is greater than 99.
This command will print the text "No." because 100 is not less than 99.
This command will print "0" because the expression is true; the two strings are identical.
[ 5 -eq 6 ]; echo $?
This command will print "1" because the expression is false; 5 does not equal 6.