test (Unix)
test is a command-line utility found in Unix, Plan 9, and Unix-like operating systems that evaluates conditional expressions. test was turned into a shell builtin command in 1981 with UNIX System III and at the same time made available under the alternate name [.[1]
Other names | [ |
---|---|
Developer(s) | Various open-source and commercial developers |
Operating system | Unix, Unix-like, Plan 9, IBM i |
Platform | Cross-platform |
Type | Command |
License | coreutils: GPLv3+ |
Overview
The test
command in Unix evaluates the expression
parameter. In most recent shell implementations, it is a shell builtin, even though the external version still exists. In the second form of the command, the [ ]
(brackets) must be surrounded by blank spaces (this is because [
is a program and POSIX compatible shells require a space between the program name and its arguments). One must test explicitly for file names in the C shell. File-name substitution (globbing) causes the shell script to exit.
The test
command is not to be confused with the [[
reserved word that was introduced with ksh88. The latter is not a command but part of the ksh88 syntax and does not apply file-name substitution to glob expressions.
The version of test
bundled in GNU coreutils was written by Kevin Braunsdorf and Matthew Bradburn.[2] The test command has also been ported to the IBM i operating system.[3]
Syntax
test expression
or
[ expression ]
Arguments
The following arguments are used to construct this parameter:
-e FileName - FileName exists
All remaining arguments return true if the object (file or string) exists, and the condition specified is true.
-b Filename - Returns a True exit value if the specified FileName exists and is a block special file -c FileName - FileName is a character special file -d FileName - FileName is a directory -f FileName - FileName is a regular file -g FileName - FileName's Set Group ID bit is set -h FileName - FileName is a symbolic link -k FileName - FileName's sticky bit is set -L FileName - FileName is a symbolic link -p FileName - FileName is a named pipe (FIFO) -r FileName - FileName is readable by the current process -s FileName - FileName has a size greater than 0 -t FileDescriptor - FileDescriptor is open and associated with a terminal -u FileName - FileName's Set User ID bit is set
-w FileName - FileName's write flag is on. However, the FileName will not be writable on a read-only file system even if test indicates true
-x FileName - FileName's execute flag is on If the specified file exists and is a directory, theTrue
exit value indicates that the current process has permission to changecd
into the directory.
Non standard Korn Shell extensions:
file1 -nt file2 - file1 is newer than file2 file1 -ot file2 - file1 is older than file2 file1 -ef file2 - file1 is another name for file2 - (symbolic link or hard link)
String arguments
In Perl, these sections are reversed: eq
is a string operator and ==
is a numerical operator, and so on for the others.
-n String1 - the length of the String1 variable is nonzero -z String1 - the length of the String1 variable is 0 (zero) String1 = String2 - String1 and String2 variables are identical String1 != String2 - String1 and String2 variables are not identical String1 - true if String1 variable is not a null string
Number arguments
Integer1 -eq Integer2 - Integer1 and Integer2 variables are algebraically equal -ne - not equal -gt - greater than -ge - greater or equal -lt - less than -le - less or equal
Operators
test
arguments can be combined with the following operators:
! - Unary negation operator -a - Binary AND operator -o - Binary OR operator (the-a
operator has higher precedence than the-o
operator) \(Expression\) - Parentheses for grouping must be escaped with a backslash\
The -a
and -o
operators, along with parentheses for grouping, are XSI extensions[4] and are therefore not portable. In portable shell scripts, the same effect may be achieved by connecting multiple invocations of test
together with the &&
and ||
operators and parentheses.
Exit status
This command returns the following exit values:
0 - The Expression parameter is true 1 - The Expression parameter is false or missing >1 - An error occurred
Examples
1. To test whether a file is nonexistent or empty, type:
if test ! -s "$1"
then
echo $1 does not exist or is empty.
fi
If the file specified by the first positional parameter to the shell procedure, $1, does not exist or is of size 0, the test command displays the message. If $1 exists and has a size greater than 0, the test command displays nothing.
Note: There must be a space between the -s function and the file name.
The quotation marks around $1 ensure that the test works properly even if the value of $1 is a null string. If the quotation marks are omitted and $1 is the empty string, the test command displays the error message:
test: argument expected.
2. To do a complex comparison, type:
if [ "$#" -lt 2 ] || ! [ -e "$1" ]
then
exit
fi
If the shell procedure is given fewer than two positional parameters or the file specified by $1 does not exist, then the shell procedure exits. The special shell variable $# represents the number of positional parameters entered on the command line that starts this shell procedure.
See also
References
- http://www.in-ulm.de/~mascheck/bourne/#system3 Bourne Shell changes with System III
- test(1) — coreutils — Debian buster — Debian Manpages
- IBM. "IBM System i Version 7.2 Programming Qshell" (PDF). Retrieved 2020-09-05.
- IEEE Std 1003.1, 2004, documentation for
test
Further reading
- Robbins, Arnold; Nelson H. F. Beebe (2005). "6.2.4 The test Command". Classic Shell Scripting: Hidden Commands that Unlock the Power of Unix. O'Reilly Media, Inc. pp. 120–128. ISBN 978-0-596-55526-9.
- Ian Shields (20 February 2007). "Linux tip: Bash test and comparison functions". IBM DeveloperWorks.
- William Shotts (2013). "27 – Flow Control: Branching With if". The Linux Command Line. No Starch Press. pp. 381–390. ISBN 978-1-59327-389-7. (free download)
External links
The Wikibook Guide to Unix has a page on the topic of: Commands |
- – Commands & Utilities Reference, The Single UNIX Specification, Issue 7 from The Open Group
- – Linux User Commands Manual
- – Plan 9 Programmer's Manual, Volume 1