TrumanWong

test

Execute conditional expression.

Summary

test [expr]

The main purpose

  • Execute conditional expressions.

Parameters

File operators:

-a FILE true if the file exists.
-b FILE true if the file is block special.
-c FILE true if the file is special characters.
-d FILE true if the file is a directory.
-e FILE true if the file exists.
-f FILE true if the file exists and is a regular file.
-g FILE True if the file is a set-group-id.
-h FILE true if the file is a symbolic link.
-L FILE true if the file is a symbolic link.
-k FILE True if the file's sticky bit is set.
-p FILE true if the file is a named pipe.
-r FILE true if you can read the file.
-s FILE true if the file exists and is not empty.
-S FILE true if the file is a socket.
-t FD True if FD is turned on on the terminal.
-u FILE True if the file is set-user-id.
-w FILE true if the file is writable.
-x FILE true if you can execute the file.
-O FILE True if the file is effectively owned by you.
-G FILE True if the file is effectively owned by your group.
-N FILE True if the file has been modified since it was last read.
    
FILE1 -nt FILE2 True if file1 is newer than file2 based on modification date.
FILE1 -ot FILE2 True if file1 is older than file2 based on modification date.
FILE1 -ef FILE2 True if file1 is a hard link to file2.

String operators:

-z STRING true if the string is empty.
-n STRING true if the string is not empty.
STRING True if the string is not empty.
STRING1 = STRING2 true if the strings are equal.
STRING1! = STRING2 true if the strings are not equal.
STRING1 < STRING2 True if STRING1 is lexicographically sorted before STRING2.
STRING1 > STRING2 True if STRING1 is lexicographically sorted after STRING2.

Other operators:

-o OPTION true if shell option OPTION is enabled.
-v VAR true if the shell variable VAR is set.
-R VAR True if the shell variable VAR is set and is a variable reference.
! EXPR true if expr is false.
EXPR1 -a EXPR2 True if both expr1 and expr2 are true.
EXPR1 -o EXPR2 True if expr1 or expr2 is true.
arg1 OP arg2 Arithmetic expression test; OP is one of -eq, -ne, -lt, -le, -gt, -ge; returns true when the arithmetic expression is true.

return value

Returns 0 if the expression execution result is successful, and returns 1 if the expression execution result is failure or illegal parameters are given.

example

#Execute the conditional expression and display the return value.
[root@pc root]$ test ! "abc" == 123; echo $?
0

# Equivalent form, note: the space after the square bracket [ and the space before the square bracket ].
[root@pc root]$ [ ! "abc" == 123 ]; echo $?
0

[root@pc root]$ [[ ! "abc" == 123 ]]; echo $?
0

Notice

  1. This command is equivalent to [.
  2. To write bash conditional expressions, you can use the built-in commands test, [, and the combined command [[;
    • Regarding conditional expressions, you can view here;
    • The index of built-in commands can be viewed here;
    • The index of combined commands can be viewed here
  3. This command is a built-in bash command. For related help information, please see the help command.