The test
command is a built-in command in Linux that evaluates the conditional expression and returns a true or false value. It is also known as [
command as it is implemented as a symbolic link to the test
command. The command is commonly used in shell scripts to test the values of variables or files.
Overview
The test
command is used to evaluate conditional expressions and return a true or false value. The syntax of the test
command is as follows:
test EXPRESSION
The EXPRESSION
can be any of the following:
- String Comparison:
string1 = string2
,string1 != string2
,string1 < string2
,string1 > string2
,string1 -n string2
,string1 -z string2
- File Comparison:
file1 -ef file2
,file1 -nt file2
,file1 -ot file2
,-b file
,-c file
,-d file
,-e file
,-f file
,-g file
,-h file
,-k file
,-p file
,-r file
,-s file
,-t file
,-u file
,-w file
,-x file
,-O file
,-G file
- Numeric Comparison:
num1 -eq num2
,num1 -ne num2
,num1 -lt num2
,num1 -le num2
,num1 -gt num2
,num1 -ge num2
- Logical Operators:
! EXPRESSION
,EXPRESSION1 -a EXPRESSION2
,EXPRESSION1 -o EXPRESSION2
The test
command returns a true (0) or false (1) value based on the evaluation of the EXPRESSION
. A true value means that the expression is true, and a false value means that the expression is false.
Examples:
To check if a file exists, you can use the following command:
test -e /path/to/file && echo "File exists" || echo "File does not exist"
To check if a string is empty, you can use the following command:
test -z "$string" && echo "String is empty" || echo "String is not empty"
To check if a number is greater than or equal to another number, you can use the following command:
test "$num1" -ge "$num2" && echo "num1 is greater than or equal to num2" || echo "num1 is less than num2"
Options
The test
command does not have any options. However, since it is implemented as a symbolic link to the test
command, it supports all the options of the test
command.
The following table lists all the available options for the test
command:
Option | Description |
---|---|
-b FILE |
True if FILE exists and is a block special file. |
-c FILE |
True if FILE exists and is a character special file. |
-d FILE |
True if FILE exists and is a directory. |
-e FILE |
True if FILE exists. |
-f FILE |
True if FILE exists and is a regular file. |
-g FILE |
True if FILE exists and is set-group-ID. |
-h FILE |
True if FILE exists and is a symbolic link. |
-k FILE |
True if FILE exists and its “sticky” bit is set. |
-p FILE |
True if FILE exists and is a named pipe (FIFO). |
-r FILE |
True if FILE exists and is readable. |
-s FILE |
True if FILE exists and has a size greater than zero. |
-t FD |
True if file descriptor FD is open and refers to a terminal. |
-u FILE |
True if FILE exists and its set-user-ID bit is set. |
-w FILE |
True if FILE exists and is writable. |
-x FILE |
True if FILE exists and is executable. |
-O FILE |
True if FILE exists and is owned by the effective user ID. |
-G FILE |
True if FILE exists and is owned by the effective group ID. |
FILE1 -ef FILE2 |
True if FILE1 and FILE2 refer to the same device and inode numbers. |
FILE1 -nt FILE2 |
True if FILE1 is newer than FILE2. |
FILE1 -ot FILE2 |
True if FILE1 is older than FILE2. |
STRING1 = STRING2 |
True if the strings are equal. |
STRING1 != STRING2 |
True if the strings are not equal. |
STRING1 < STRING2 |
True if STRING1 is less than STRING2. |
STRING1 > STRING2 |
True if STRING1 is greater than STRING2. |
-n STRING |
True if the length of STRING is non-zero. |
-z STRING |
True if the length of STRING is zero. |
NUM1 -eq NUM2 |
True if NUM1 is equal to NUM2. |
NUM1 -ne NUM2 |
True if NUM1 is not equal to NUM2. |
NUM1 -lt NUM2 |
True if NUM1 is less than NUM2. |
NUM1 -le NUM2 |
True if NUM1 is less than or equal to NUM2. |
NUM1 -gt NUM2 |
True if NUM1 is greater than NUM2. |
NUM1 -ge NUM2 |
True if NUM1 is greater than or equal to NUM2. |
! EXPRESSION |
True if EXPRESSION is false. |
EXPRESSION1 -a EXPRESSION2 |
True if both EXPRESSION1 and EXPRESSION2 are true. |
EXPRESSION1 -o EXPRESSION2 |
True if either EXPRESSION1 or EXPRESSION2 is true. |
Troubleshooting Tips
- If you are getting unexpected results, double-check the syntax of the
EXPRESSION
. - When using variables in the
EXPRESSION
, make sure to enclose them in double-quotes to prevent word splitting and globbing. - When using the
-n
or-z
options, make sure to enclose theSTRING
in double-quotes to prevent globbing.
Notes
- The
test
command is often used in shell scripts to test the values of variables or files. - The
test
command is implemented as a symbolic link to thetest
command, and therefore supports all the options of thetest
command. - The
test
command can be used in conjunction with other commands, such asif
,while
, anduntil
.