Linux test command: Comparisons in Bash
The test
command in Linux is a command-line utility used to check and evaluate conditions.
It’s often used in Bash scripts to test file attributes, compare strings and numbers, and support complex logical evaluations with AND, OR, and NOT operations.
Syntax & Return Values
The syntax of the test
command is as follows:
test expression
or
[ expression ]
Return Values:
- 0: The expression is true.
- 1: The expression is false.
- 2: An error occurred in the test command, such as a syntax error.
The return value can be checked using the $?
variable.
test 5 -eq 5 echo $?
Output:
0
This example checks if the integer 5 is equal to 5. The expression is true, so the return value is 0.
Here’s a list of options for the test
command:
Option | Description |
---|---|
-e | Check if file exists |
-d | Check if file is a directory |
-f | Check if file is a regular file |
-h or -L | Check if file is a symbolic link |
-r | Check if file is readable |
-w | Check if file is writable |
-x | Check if file is executable |
-s | Check if file is empty |
-nt
-ot |
Compare file timestamps (newer than, older than) |
-z | Check if string is null |
-n | Check if string is not null |
= | Check if strings are equal |
!= | Check if strings are not equal |
< | Lexicographical comparison (less than) |
> | Lexicographical comparison (greater than) |
-eq | Equal |
-ne | Not Equal |
-gt | Greater Than |
-lt | Less Than |
-ge | Greater Than or Equal |
-le 5 | Less Than or Equal |
-a | Logical AND |
-o | Logical OR |
! | Logical NOT |
File Testing
File testing is one of the primary uses of the test
command. You can evaluate various properties and conditions of files using specific operators.
Check if File Exists
test -e /path/to/file echo $?
Output:
0
If the file exists at the specified path, the return value will be 0. If the file doesn’t exist, the return value will be 1.
Check if File is a Directory
You can use the -d
option to check if the path points to a directory:
test -d /path/to/directory echo $?
Output:
0
A return value of 0 indicates that the path points to a directory, while a return value of 1 indicates otherwise.
Check if File is a Regular File
Using -f
option with the test
command, you can check if the file is a regular file.
test -f /path/to/file echo $?
Output:
0
A return value of 0 means the path is a regular file, while a return value of 1 means it is not.
Check if file is a Symbolic Link
Symbolic links are shortcuts or references to other files.
Using the -h
or -L
option with the test
command, you can verify whether a specified path is a symbolic link or not.
Command:
test -L /path/to/symbolic_link echo $?
Output:
0
A return value of 0 confirms that it is a symbolic link, whereas a return value of 1 indicates that it is not.
Check if File is Readable
Using the -r
option with the test
command, you can check whether a particular file is readable by the current user.
Command:
test -r /path/to/file echo $?
Output:
0
A return value of 0 means the file is readable, while a return value of 1 indicates that it is not readable.
Check if File is Writable
Understanding if a file is writable is crucial when you need to make changes to it.
The -w
option with the test
command allows you to determine whether the current user has write permissions for a specific file.
Command:
test -w /path/to/file echo $?
Output:
0
A return value of 0 means the file is writable, while a return value of 1 signifies that it is not writable.
Check if File is Executable
By using the -x
option with the test
command, you can check if a specific file is executable by the current user.
test -x /path/to/executable_file echo $?
Output:
0
A return value of 0 indicates the file is executable, while a return value of 1 means that it is not executable.
Check if File is Empty
By using the -s
option with the test
command, you can determine whether a specified file is empty or not.
test -s /path/to/file echo $?
Output:
0
A return value of 0 means that the file has some content, while a return value of 1 means that the file is empty.
Compare File Timestamps
Comparing file timestamps is useful for many operations, such as backups or synchronization.
Using -nt
and -ot
with the test
command, you can compare the timestamps of two files.
-nt (Newer Than)
test file1 -nt file2 echo $?
Output:
0
This command checks if file1
is newer than file2
. A return value of 0 indicates that file1
is indeed newer, while a return value of 1 means that it is not.
-ot (Older Than)
test file1 -ot file2 echo $?
Output:
0
This command checks if file1
is older than file2
. A return value of 0 indicates that file1
is older, while a return value of 1 means that it is not.
String Testing
The test
command provides various options for comparing or evaluating text-based values.
Check if String is Null
By using the -z
option, you can check whether a particular string is null.
test -z "" echo $?
Output:
0
A return value of 0 means that the string is null, while a return value of 1 means that it is not.
Check if String is Not Null
The -n
option with the test
command allows you to verify whether a specific string is not null.
test -n "Hello" echo $?
Output:
0
A return value of 0 indicates that the string is not null, while a return value of 1 means that it is null.
String Comparison
Using the =
and !=
options with the test
command, you can compare two strings for equality or inequality.
= (Equal)
Command:
test "string1" = "string1" echo $?
Output:
0
A return value of 0 means the strings are equal, while a return value of 1 means they are not.
!= (Not Equal)
Command:
test "string1" != "string2" echo $?
Output:
0
This checks if the two strings are not equal. A return value of 0 indicates that the strings are indeed not equal, while a return value of 1 means that they are equal.
Lexicographical Comparison
Lexicographical comparisons are used to compare strings based on their dictionary order.
With the test
command, you can conduct these comparisons using the <
and >
operators.
< (Less Than)
test "apple" \< "banana" echo $?
Output:
0
This checks if the string “apple” comes before “banana” in dictionary order. A return value of 0 means that it does, while a return value of 1 means that it does not.
> (Greater Than)
test "banana" \> "apple" echo $?
Output:
0
This checks if the string “banana” comes after “apple” in dictionary order. A return value of 0 indicates that it does, while a return value of 1 means that it does not.
Integer Comparison
The test
command provides various options for integer comparisons.
Equal
test 5 -eq 5 echo $?
Output:
0
This checks if the two integers are equal. A return value of 0 means the numbers are equal, while a return value of 1 means they are not.
Not Equal
test 5 -ne 10 echo $?
Output:
0
This checks if the two integers are not equal. A return value of 0 indicates that the numbers are indeed not equal, while a return value of 1 means that they are equal.
Greater Than
test 10 -gt 5 echo $?
Output:
0
This checks if the first integer is greater than the second one. A return value of 0 means that the first number is greater, while a return value of 1 means that it is not.
Less Than
test 5 -lt 10 echo $?
Output:
0
This checks if the first integer is less than the second one. A return value of 0 indicates that the first number is less, while a return value of 1 means that it is not.
Greater Than or Equal
test 10 -ge 10 echo $?
Output:
0
This checks if the first integer is greater than or equal to the second one. A return value of 0 means that the first number is greater or equal, while a return value of 1 means that it is not.
Less Than or Equal
The -le
option allows you to check if one integer is less than or equal to another.
test 5 -le 5 echo $?
Output:
0
A return value of 0 indicates that the first number is indeed less or equal, while a return value of 1 means that it is not.
Compound Testing
Compound testing combines multiple test conditions, allowing for more complex logic. Logical operators such as AND, OR, and NOT can be used with the test
command for this purpose.
Logical AND
test 5 -lt 10 -a 5 -gt 2 echo $?
Output:
0
This checks if 5 is less than 10 AND greater than 2. Both conditions must be true.
A return value of 0 indicates that both conditions are met, while a return value of 1 means that at least one condition is not met.
Logical OR
test 5 -lt 3 -o 5 -gt 2 echo $?
Output:
0
This checks if 5 is less than 3 OR greater than 2. At least one condition must be true.
A return value of 0 indicates that at least one condition is met, while a return value of 1 means that neither condition is met.
Logical NOT
test ! 5 -lt 3 echo $?
Output:
0
This checks if 5 is NOT less than 3. A return value of 0 means that the NOT condition is met (5 is not less than 3), while a return value of 1 means that it is not met.
Group Testing with Parentheses
You can group testing with parentheses. It helps in controlling the order of evaluation in complex expressions.
test \( 5 -lt 10 \) -a \( 5 -gt 2 \) echo $?
Output:
0
This checks if 5 is less than 10 AND greater than 2, with the conditions grouped using parentheses.
The parentheses ensure that the individual comparisons are evaluated first, and then the logical AND is applied.
A return value of 0 indicates that both conditions are met, while a return value of 1 means that at least one condition is not met.
Using [ ] as alternative
The square brackets [ ]
can be used interchangeably with the test
command. Here’s how the (greater than) can be rewritten using [ ]
:
[ $num1 -gt $num2 ] echo $?
You can use it in a Bash script like this:
#!/bin/bash num1=$1 num2=$2 if [ $num1 -gt $num2 ]; then echo "Number1 is greater than Number2." else echo "Number1 is not greater than Number2." fi
The script accomplishes the same task as the previous example by checking if the first number is greater than the second one.
Extended test using [[ expression ]]
The [[
command is an extended test command, providing more features and often more natural syntax than test
command.
It’s particularly helpful for pattern matching and more complex expressions.
[[ 5 -lt 10 && "apple" < "banana" ]] echo $?
Output:
0
This checks if 5 is less than 10 AND “apple” comes before “banana” in dictionary order.
The double square brackets [[
provide a more flexible way to combine tests.
A return value of 0 indicates that both conditions are met, while a return value of 1 means that at least one condition is not met.
Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology.