The test command in Linux



The test command in Linux evaluates the conditional expression and checks the file types. It is commonly used in shell scripts to perform checks like comparing numbers or strings, and verifying file attributes such as existence, type, and permissions.

Table of Contents

Here is a comprehensive guide to the options available with the test command in Linux −

Syntax of test Command

The syntax of the test command in Linux is as follows −

test [options] [expression]

In the above syntax, the [options] argument is optional used to specify options to view the command's help and version. The [expression] argument is used to specify the expression that needs to be evaluated.

Options of test Command

The options of the test command are listed below −

Option Description
--help Display help and exit
--version Output version information and exit

Expressions of test Command

The following expressions are used with the test command −

Expression Description
( EXPRESSION ) EXPRESSION is true
! EXPRESSION EXPRESSION is false
EXPR1 -a EXPR2 Both expressions are true
EXPR1 -o EXPR2 At least one expression is true
-n STRING STRING length is non-zero
STRING Same as -n STRING
-z STRING STRING length is zero
STRING1 = STRING2 Strings are equal
STRING1 != STRING2 Strings are not equal
STRING1 > STRING2 STRING1 is greater (locale-based)
STRING1 < STRING2 STRING1 is less (locale-based)
INT1 -eq INT2 Equal integers
INT1 -ge INT2 INT1 >= INT2
INT1 -gt INT2 INT1 > INT2
INT1 -le INT2 INT1 <= INT2
INT1 -lt INT2 INT1 < INT2
INT1 -ne INT2 INT1 != INT2
FILE1 -ef FILE2 Same device and inode
FILE1 -nt FILE2 FILE1 is newer than FILE2
FILE1 -ot FILE2 FILE1 is older than FILE2
-b FILE FILE is a block special file
-c FILE FILE is a character special file
-d FILE FILE is a directory
-e FILE FILE exists
-f FILE FILE is a regular file
-g FILE FILE has set-group-ID
-G FILE Owned by effective group ID
-h FILE Symbolic link (same as -L)
-k FILE The sticky bit is set
-L FILE Symbolic link (same as -h)
-N FILE Modified since last read
-O FILE Owned by effective user ID
-p FILE Named pipe (FIFO)
-r FILE Readable file
-s FILE Non-empty file
-S FILE Socket file
-t FD FD is a terminal
-u FILE Set-user-ID is set
-w FILE Writable file
-x FILE Executable or searchable

Using test Command in Linux

This section discusses how to use the test command in Linux with examples −

Testing Files using the test Command

To check whether the file exists or not, use the -e option followed by the filename −

test -e file.txt

The test command does not display output, but it returns a 0 exit code if the condition is true, and 1 if false. For instance, in the above example, if the file exists, the exit code will be 0, else 1. To check the exit code, run the following command −

echo $?
test Command in Linux1

Since the file exists in the current working directory, the exit code is 0, as shown in the above output image.

To check if the specified argument is a directory or not, use the -d option −

test -d /etc
test Command in Linux2

To check if a file contains content or not, use the -s option −

test -s file.txt
test Command in Linux3

To check if the file is writable or not, use the -w option −

test -w file.txt

Comparing Strings using the test Command

To check if two strings are equal or contain the same content, use the test command in the following way −

test "$str1" = "$str2"

In this case, the exit code will be 1, because the two strings do not match, as shown in the following output image −

test Command in Linux4

Checking if a string is empty or not −

test -z "$str1"
test Command in Linux5

Comparing Integers using the test Command

To compare if a number is less than the second number, use the test command in the following way −

test "$num1" -lt "$num2"
test Command in Linux6

Since 3 is less than 5, the exit code is 0 as shown in the above image.

To check if a number is equal to another number, use the following command −

test "$num1" -eq "$num2"
test Command in Linux7

Using Logical Operators with the test Command

Integers, strings, and files can also be compared using the logical operators with the test command. For example, to check if the file exists and is readable, use the following command −

test -f file.txt && test -r file.txt

The square brackets are aliases of the test command, so the above command can also be executed in the following way −

[ -f file.txt ] && [ -r file.txt ]
test Command in Linux8

The exit code of the above command will be 0 if the file.txt exists in the current directory and is readable, otherwise 1.

Similarly, the exit status of the following command will be 0 if any of the specified conditions are true −

[ "$num1" -gt 0 ] || [ "$num2" -lt 5 ]
test Command in Linux9

Displaying Usage Help

If the --help option is used with the test command, then --help is treated as a string. To avoid it, use the /usr/bin/[ followed by --help option to display the usage help −

/usr/bin/[ --help

Conclusion

The test command in Linux is used to evaluate expressions and check file types, attributes, strings, and integers. It is a fundamental tool in shell scripting for making decisions based on conditions.

The test command returns an exit code instead of producing output, where 0 indicates success (true) and 1 indicates failure (false). By using various options and logical operators, different file properties, string comparisons, and numeric conditions can be tested efficiently.

Advertisements