← New search

Other meanings of test

Computing

Test (Unix)

In Unix and Unix-like operating systems, test is a command-line utility that evaluates conditional expressions and returns an exit status indicating whether the expression is true or false. It is commonly used in shell scripts to make decisions, and it is also known by its alias [ (left bracket), which requires a closing bracket. The command is a standard part of the POSIX specification and is implemented as a built-in in most shells, though a standalone executable also exists.

1969
Year introduced
test appeared in early Unix research versions
1
Exit status for true
Returns 0 for true, 1 for false, 2 for error
POSIX
Standard
Defined in POSIX.1-2017
1

Overview and syntax

The test command evaluates an expression and returns an exit status of 0 (true) or 1 (false). It is often used in shell scripts within if, while, and until constructs. The basic syntax is test expression or [ expression ]. The latter form requires a space after the opening bracket and before the closing bracket. The command supports a wide range of unary and binary operators for file attributes, string comparisons, and integer comparisons.1

2

History and standardization

The test command appeared in early Unix versions, with the bracket alias [ introduced later as a link to the same executable. It was standardized by POSIX and is now part of the POSIX.1-2017 specification. The command is implemented as a shell built-in in most modern shells (e.g., bash, zsh, dash) for efficiency, but a standalone binary is still provided for compatibility.2

3

Common operators

The command supports file operators such as -e (exists), -f (regular file), -d (directory), and -r (readable). String operators include -z (empty string), -n (non-empty), and comparison operators = and !=. Integer comparisons use -eq, -ne, -lt, -le, -gt, and -ge. Logical operators are ! (negation), -a (and), and -o (or), though the latter two are considered obsolete and should be replaced with && and || in modern scripts.1

4

Differences between test and [

The [ command is a synonym for test, but it requires a final argument of ]. In some shells, [ is a built-in that behaves slightly differently from the external command, particularly regarding argument handling and error messages. For example, in bash, the built-in [ may treat certain expressions differently than the external test binary.3

5

Lesser-known aspects

One lesser-known fact is that the test command can be used to compare file modification times with -nt (newer than) and -ot (older than). Another is that the -t operator checks if a file descriptor is associated with a terminal, which is useful for detecting interactive sessions. Additionally, the command has a rarely used -a and -o for logical and/or, but these are not recommended due to ambiguity. The test command is also available in Windows via Unix-like environments such as Cygwin and MSYS2.

6

Portability and shell built-ins

While the external test command is defined by POSIX, many shells implement it as a built-in, which can lead to subtle differences. For example, in bash, the built-in test supports additional operators like -v (variable is set) and -R (variable is a name reference). Scripts that rely on these extensions are not portable to other shells. The [[ keyword in bash and ksh is an enhanced version that avoids word-splitting and pathname expansion issues, but it is not POSIX-compliant.4

Glossary

Exit status
A numeric code returned by a command to indicate success (0) or failure (non-zero).
Shell built-in
A command that is implemented within the shell itself, rather than as an external executable.
POSIX
A family of standards for maintaining compatibility between operating systems, defining a common API and command-line utilities.

The test command is a fundamental tool in Unix shell scripting, and its alias '[' is often used in if statements.

Served from cache