How to use the test command in Linux
Editor to share with you how to use the test command in Linux, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Linux common commands test command is a very important command in scripting. It is mainly used for file (directory) detection, attribute interpretation, authority judgment and so on.
Test executes conditional expressions.
Summary test [expr] is mainly used to execute conditional expressions.
Parameter file operator:-a FILE true if the file exists. -b FILE true if the file is block specific. -c FILE true if the file is a special character. -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 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 stickiness bit (sticky) of the file 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 opened 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 if the file is effectively owned by you, it is true. -G FILE is 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 according to the modification date, true if file1 is newer than file2. FILE1-ot FILE2 according to the modification date, true if file1 is older than file2. FILE1-ef FILE2 true if file1 is a hard link of file2. String operator:-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 string is not equal. STRING1 STRING1 > STRING2 true if STRING1 is sorted after STRING2 in the dictionary. Other operators:-o OPTION true if the 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-an 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 if the arithmetic expression is true. The return value returns 0 if the expression execution result is successful, and 1 if the expression execution result fails or gives an illegal parameter.
Example # executes the conditional expression and displays the return value. [root@pc root] $test! "abc" = = 123; echo $? 0 # equivalent, note: the space before the square brackets [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 note that this command is equivalent to [.
You can write bash conditional expressions with built-in commands test, [, combined commands []
For more information about conditional expressions, please see here
An index of built-in commands can be found here
The index of the combination command can be found here
This command is a bash built-in command. For help, please see the help command.
The above is all the contents of the article "how to use test commands in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!