test (Unix)
   HOME

TheInfoList



OR:

test is a command-line utility found in
Unix Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
, Plan 9, and
Unix-like A Unix-like (sometimes referred to as UN*X, *nix or *NIX) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Uni ...
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 The C shell (csh or the improved version, tcsh) is a Unix shell created by Bill Joy while he was a graduate student at University of California, Berkeley in the late 1970s. It has been widely distributed, beginning with the 2BSD release of the ...
. 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 (computing), native Windows API, Win32
ports Ports collections (or ports trees, or just ports) are the sets of makefiles and Patch (Unix), patches provided by the BSD-based operating systems, FreeBSD, NetBSD, and OpenBSD, as a simple method of installing software or creating binary packages. T ...
of common GNU Unix-like utilities. The command has also been ported to the
IBM i IBM i (the ''i'' standing for ''integrated'') is an operating system developed by IBM for IBM Power Systems. It was originally released in 1988 as OS/400, as the sole operating system of the IBM AS/400 line of systems. It was renamed to i5/OS in 2 ...
operating system.


Syntax

test ''expression'' or ''expression'' /code>


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. 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 KornShell (ksh) is a Unix shell which was developed by David Korn at Bell Labs in the early 1980s and announced at USENIX on July 14, 1983. The initial development was based on Bourne shell source code. Other early contributors were Bell ...
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 In computing, a symbolic link (also symlink or soft link) is a file whose purpose is to point to a file or directory (called the "target") by specifying a path thereto. Symbolic links are supported by POSIX and by most Unix-like operating syste ...
or
hard link In computing, a hard link is a directory entry (in a Directory (computing), directory-based file system) that associates a name with a Computer file, file. Thus, each file must have at least one hard link. Creating additional hard links for a fil ...
)


String arguments

In
Perl Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language". Perl was developed ...
, 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
algebra Algebra is a branch of mathematics that deals with abstract systems, known as algebraic structures, and the manipulation of expressions within those systems. It is a generalization of arithmetic that introduces variables and algebraic ope ...
ically 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 extensionsIEEE Std 1003.1, 2004, documentation for test
/ref> 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

* List of POSIX commands *
Unix shell A Unix shell is a Command-line_interface#Command-line_interpreter, command-line interpreter or shell (computing), shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command languag ...
*
find (Unix) In Unix-like operating systems, find is a command-line utility that locates files based on some user-specified criteria and either prints the pathname of each matched object or, if another action is requested, performs that action on each mat ...


References


Further reading

* * * (free download)


External links

* * * {{Core Utilities commands Unix SUS2008 utilities Plan 9 commands IBM i Qshell commands Conditional constructs