On Unix-like operating systems, test is a builtin command of the Bash shell that tests file attributes, and perform string and arithmetic comparisons.
Description
test provides no output, but returns an exit status of 0 for “true” (test successful) and 1 for “false” (test failed).
- Description
- Syntax
- Examples
- Related commands
- Bash builtins help
- Linux commands help
The test command is frequently used as part of a conditional expression. For instance, the following statement says, “If 4 is greater than 5, output yes, otherwise output no.”
num=4; if (test $num -gt 5); then echo “yes”; else echo “no”; fi
no
The following statement says, “If 6 is greater than 5, output yes, otherwise output no.”
num=6; if (test $num -gt 5); then echo “yes”; else echo “no”; fi
yes
The test command may also be expressed with single brackets [ … ], as long as they are separated from all other arguments with whitespace. For example, the following statement checks that the system file /etc/passwd exists, and if not, outputs “uh-oh.”
file="/etc/passwd"; if [ -e $file ]; then echo “whew”; else echo “uh-oh”; fi
whew
Syntax
File tests:
test [-a] [-b] [-c] [-d] [-e] [-f] [-g] [-h] [-L] [-k] [-p] [-r] [-s] [-S] [-u] [-w] [-x] [-O] [-G] [-N] [file]
test -t fd
test file1 {-nt | -ot | -ef} file2
String tests:
test [-n | -z] string
test string1 {= | != | < | >} string2
Shell options and variables:
test -o option
test {-v | -R} var
Simple logic (test if values are null):
test [!] expr
test expr1 {-a | -o} expr2
Numerical comparison (for integer values only; bash doesn’t do floating point math):
test arg1 {-eq | -ne | -lt | -le | -gt | -ge} arg2
Options
The test builtin command takes the following options.
Notes
All arguments to test must be separated by a space, including all operators.
The < and > operators are lexicographical comparisons, based on ASCII numbering. They are not numerical operators (instead, use -lt, -gt, etc. for comparing numbers).
The precise behavior of test, depending on the number of arguments provided, is as follows:
Exit status
0 for true, 1 for false. Anything greater than 1 indicates an error or malformed command.
Examples
test
This command returns 1 for false, because it has no arguments to test. (See notes above for how test behaves with various numbers of arguments.) This is good, because the only time test should ever run with no arguments is when something has gone wrong in your script or command.
To confirm that test returned false, we’ll check the value of the special shell variable ?, which contains the exit status of the previous command executed.
echo $?
1
Here are a few more examples.
if test false; then echo “Test always returns true for only one argument, unless it is null.”; fi
Test always returns true for only one argument, unless it is null.
[ false ]; echo $? # always true for one argument if not null
0
[ true ]; echo $? # same as previous command
test -e /sys; echo $? # true if file or directory exists
test -e /does-not-exist; echo $?
test -c /dev/uinput; echo $? # true for character-special devices
if test -d /home; then echo “/home is a directory”; fi
/home is a directory
test -f /etc/shadow; echo $? # true for regular files
test -f /etc; echo $? # false for directories
test -h /dev/stdin; echo $? # true for symbolic links
pn=“MyPipe”; mkfifo “$pn”; test -p mypipe; if [ $? -eq 0 ]; then echo “$pn is a named pipe”; fi
MyPipe is a named pipe
test -r /etc/motd; echo $? # true if file is readable by current user
file="/etc/passwd"; if [ -s “$file” ]; then echo “$file exists, and is not empty”; fi
/etc/passwd exists, and is not empty
touch newfile; if [ -O newfile ]; then echo “I own newfile”; fi
I own newfile
str=""; test -z “$str”; echo $? # true if variable str is empty string ""
str=""; test -n “$str”; echo $? # true if variable str is not empty string ""
str=""; test “$str”; echo $? # same as above command; -n is optional
str=“Not empty”; test -z “$str”; echo $?
str=“Not empty”; test -n “$str”; echo $?
str=“Not empty”; test “$str”; echo $?
if test -x /bin/bash; then echo “/bin/bash is executable”; fi
/bin/bash is executable
str=“Match”; if [ “$str” = “Match” ]; then echo “The strings match.”; else echo “The strings do not match.”; fi
The strings match.
str=“match”; if [ “$str” = “Match” ]; then echo “The strings match.”; else echo “The strings do not match.”; fi
The strings do not match.
test “Apple” < “Banana”; echo $? # true because ASCII 65 (A) < 66 (B)
test “apple” < “Banana”; echo $? # false because ASCII 97 (a) is not < 66 (B)
test 5 -gt 6; echo $? # false because 5 is not greater than 6
test 6 -gt 5; echo $? # true
test “6” -gt “5”; echo $? # quoted numbers are properly compared
test 5.5 -gt 5.4; echo $? # error: bash doesn’t do floating point math
bash: test: 5.5: integer expression expected 2
Related commands
df — View used and available disk space.free — View used and available memory.