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 [.

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. The command is available as a separate package for Microsoft Windows as part of the UnxUtils collection of native Win32 ports of common GNU Unix-like utilities. The test command has also been ported to the IBM i operating system.

Syntax

test expression or [ expression ]

Arguments

The following arguments are used to construct this parameter. All arguments return True if the object (file or string) exists, and the condition specified is true.

ArgumentReturns True if the file
-bis a block special file
-cis a character special file
-dis a directory
-eexists
-fis a regular file
-ghas the Set Group ID bit set
-his a symbolic link
-khas the sticky bit set
-Lis a symbolic link
-pis a named pipe (FIFO)
-ris readable by the current process
-shas a size greater than 0
-tFileDescriptor is open and associated with a terminal
-uhas the Set User ID bit set
-whas the write flag is on
-xhas execute flag on

For the -x argument, if the specified file exists and is a directory, the True exit value indicates that the current process has permission to change cd into the directory.

Non standard Korn Shell extensions

String arguments

In Perl, these sections are reversed: eq is a string operator and == is a numerical operator, and so on for the others.

Number arguments

Operators

test arguments can be combined with the following operators:

The -a and -o operators, along with parentheses for grouping, are XSI extensions 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:

Examples

1. To test whether a file is nonexistent or empty, type:

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:

2. To do a complex comparison, type:

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

Further reading

  • Robbins, Arnold; Nelson H. F. Beebe (2005). . 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). . IBM DeveloperWorks.
  • William Shotts (2013). "27 – Flow Control: Branching With if". . No Starch Press. pp. 381–390. ISBN 978-1-59327-389-7. (free download)

External links