test (Unix)
   HOME

TheInfoList



OR:

test is a
command-line A command-line interpreter or command-line processor uses a command-line interface (CLI) to receive commands from a user in the form of lines of text. This provides a means of setting parameters for the environment, invoking executables and pro ...
utility found in
Unix Unix (; trademarked as UNIX) is a family of multitasking, multiuser 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, and ot ...
, Plan 9, and
Unix-like A Unix-like (sometimes referred to as UN*X 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 Unix-li ...
operating systems that evaluates
conditional expressions Conditional (if then) may refer to: *Causal conditional, if X then Y, where X is a cause of Y *Conditional probability, the probability of an event A given that another event B has occurred *Conditional proof, in logic: a proof that asserts a co ...
. 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 In computer programming, glob () patterns specify sets of filenames with wildcard characters. For example, the Unix Bash shell command mv *.txt textfiles/ moves (mv) all files with names ending in .txt from the current directory to the director ...
) 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 A port is a maritime facility comprising one or more wharves or loading areas, where ships load and discharge cargo and passengers. Although usually situated on a sea coast or estuary, ports can also be found far inland, such as H ...
of common GNU Unix-like utilities. The 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: -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 In Unix-like operating systems, a device file or special file is an interface to a device driver that appears in a file system as if it were an ordinary file. There are also special files in DOS, OS/2, and Windows. These special files allow ...
-d FileName - FileName is a
directory Directory may refer to: * Directory (computing), or folder, a file system structure in which to store computer files * Directory (OpenVMS command) * Directory service, a software application for organizing information about a computer network's u ...
-f FileName - FileName is a regular file -g FileName - FileName's Set Group ID bit is set -h FileName - FileName is a
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 system ...
-k FileName - FileName's sticky bit is set -L FileName - FileName is a
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 system ...
-p FileName - FileName is a
named pipe In computing, a named pipe (also known as a FIFO for its behavior) is an extension to the traditional pipe concept on Unix and Unix-like systems, and is one of the methods of inter-process communication (IPC). The concept is also found in OS/2 and ...
(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 Terminal may refer to: Computing Hardware * Terminal (electronics), a device for joining electrical circuits together * Terminal (telecommunication), a device communicating over a line * Computer terminal, a set of primary input and output dev ...
-u FileName - FileName's Set
User ID Unix-like operating systems identify a user by a value called a user identifier, often abbreviated to user ID or UID. The UID, along with the group identifier (GID) and other access control criteria, is used to determine which system resources a us ...
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 Execute, in capital punishment, is to put someone to death. Execute may also refer to: * Execution (computing), the running of a computer program * ''Execute'' (album), a 2001 Garage hip-hop album by Oxide & Neutrino * USS ''Execute'' (AM-232), a ...
flag is on 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 system ...
or
hard link In computing, a hard link is a directory entry (in a directory-based file system) that associates a name with a file. Thus, each file must have at least one hard link. Creating additional hard links for a file makes the contents of that file acc ...
)


String arguments

In
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offici ...
, 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 one of the broad areas of mathematics. Roughly speaking, algebra is the study of mathematical symbols and the rules for manipulating these symbols in formulas; it is a unifying thread of almost all of mathematics. Elementary a ...
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 Unix commands This is a list of Unix commands as specified by IEEE Std 1003.1-2008, which is part of the Single UNIX Specification (SUS). These commands can be found on Unix operating systems and most Unix-like operating systems. List See also * List of G ...
*
Unix shell A Unix shell is a command-line Interpreter (computing), interpreter or shell (computing), shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command language and a scripting langua ...
*
find (Unix) In Unix-like and some other 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 acti ...


References


Further reading

* * * (free download)


External links

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