← New search

Other meanings of test

Computing

Test (group)

In Unix-like operating systems, the test command (also known as [) is a utility that evaluates conditional expressions and returns an exit status indicating whether the expression is true or false. It is a fundamental tool in shell scripting, used for file attribute checks, string comparisons, and arithmetic comparisons.

1971
First appeared in Unix V1
test
POSIX
Standardized by IEEE
test
0/1
Exit statuses (true/false)
test
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 command has two forms: test expression and the equivalent [ expression ] (with a closing bracket).1 The latter form is more common in scripts, but requires a space after the opening bracket and before the closing bracket.

2

History and standardization

The test command first appeared in Unix V1 (1971) as a way to test file attributes and string comparisons. It was later standardized by POSIX (IEEE 1003.1) and the Single UNIX Specification, which define its behavior and options. The [ alias is implemented as a built-in in most shells (e.g., bash, zsh) but also exists as an external executable in some systems.

3

Common use cases

Typical uses include checking if a file exists (-e), if it is a regular file (-f), if it is readable (-r), comparing strings (=, !=, -z for empty), and comparing integers (-eq, -ne, -lt, -le, -gt, -ge).1 For example, [ -f "/etc/passwd" ] returns true if the file exists and is a regular file.

4

Lesser-known aspects

One lesser-known fact is that the [ command is actually a synonym for test, but it requires a final argument of ]; this is why the syntax is [ ... ].2 Another is that in some shells, test is a built-in, but the external version still exists in /usr/bin/test. Additionally, the [[ keyword in bash and zsh is an enhanced version that supports pattern matching and regular expressions, but it is not POSIX-compliant.3 The test command also has a rarely used -a (logical AND) and -o (logical OR) for combining expressions, though these are considered obsolete and are not recommended for new scripts.

5

Portability and pitfalls

When writing portable shell scripts, it is important to rely on POSIX features of test. Some implementations may differ in handling of empty strings or unset variables; for example, [ "$var" = "" ] is safer than [ -z "$var" ] in some edge cases.4 Also, the -a and -o operators are ambiguous and can cause issues; using multiple test commands with && or || is preferred.

Glossary

Exit status
A numeric value 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 specified by the IEEE for maintaining compatibility between operating systems.

The test command is a cornerstone of shell scripting, often used implicitly in every if statement.

Served from cache