Intro To 'test' Command In Linux
2023-06-11 - By Robert Elder
I use the 'test' command to test for file types and evaluate expressions:
test -f report.txt
echo $?
0
Equality Expressions
I can use the 'test' command to determine if two numbers are equal. The result of the test is found in the return code of the 'test' command. You can view the return code by echoing the dollar sign question mark variable:
test 123 -eq 123
echo $?
0
test 123 -eq 125
echo $?
1
Note that a return code of 0 is used to indicate success, while a return code of 1 is used to indicate failure. These values have an opposite meaning compared to the traditional values found in boolean logic.
The 'test' command supports many other comparison operators, as you'll see demonstrated in the next few sections.
Less Than & Greater Than Comparison
# Is 123 Greater Than 125?
test 123 -gt 125
echo $?
1
# Is 123 Less Than 125?
test 123 -lt 125
echo $?
0
# Is 123 Less Than or Equal To 123?
test 123 -le 123
echo $?
0
# Is 123 Greater Than or Equal To 123?
test 123 -ge 123
echo $?
0
Equality & Emptiness Checks For Strings
# Check if 'the length of STRING is nonzero'
test -n "non empty"
echo $?
0
# Check if 'the length of STRING is zero'
test -z "non empty"
echo $?
1
# Check if 'the length of STRING is nonzero'
test -n ""
echo $?
1
# Check if 'the length of STRING is zero'
test -z ""
echo $?
0
# Check for string equality
test "foo" = "foo"
echo $?
0
# Check for string equality
test "foo" = "boo"
echo $?
1
# Check for string inequality
test "foo" != "foo"
echo $?
1
# Check for string inequality
test "foo" != "boo"
echo $?
0
Boolean Operators
# '-o' For Logical 'or'
test 123 -gt 123 -o 345 -eq 345
echo $?
0
# '-a' For Logical 'and'
test 3 -eq 3 -a 3 -eq 3
echo $?
0
# '!' For Logical 'not'
test ! 1 -eq 0
echo $?
0
# '!' For Logical 'not'
test ! 1 -eq 1
echo $?
1
File Operators
The '-f' flag can be used to test if a file exists:
# Test if 'abc exists and is a regular file'
test -f abc
The '-d' flag can be used to test if a directory exists:
# Test if 'def exists and is a directory'
test -d def
Shell Builtin Vs. Standalone Executable
The 'test' command can be built into the shell or it can exist as a standalone executable. Therefore, there can be subtle differences in the behaviour of 'test' depending which version you're using:
type -a test
test is a shell builtin
test is /usr/bin/test
test is /bin/test
And that's why the 'test' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|