test is a
command-line
A command-line interpreter or command-line processor uses a command-line interface (CLI) to receive command (computing), commands from a user in the form of lines of text. This provides a means of setting parameters for the environment, invokin ...
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, a ...
,
Plan 9 Plan 9 or Plan Nine may refer to:
Music
* Plan 9 (band), a psychedelic rock band from Rhode Island
* ''Plan 9'', an album by Big Guitars From Memphis with Rick Lindy
* "Plan 9", a song on the 1993 album ''Gorgeous'' by electronica band 808 Stat ...
, 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 ...
. 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">''.
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 B ...
. 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 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 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' ...
-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 syste ...
-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 syste ...
-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/ ...
(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
-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
Capital punishment, also known as the death penalty, is the state-sanctioned practice of deliberately killing a person as a punishment for an actual or supposed crime, usually following an authorized, rule- ...
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 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-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 ac ...
)
String arguments
In
Perl
Perl is a family of two High-level programming language, high-level, General-purpose programming language, general-purpose, Interpreter (computing), interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it ...
, 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 areas of mathematics, 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 mathem ...
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 extensions
IEEE 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 or shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command language and a scripting language, and is used by the operating system t ...
* 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