Test (Unix)
Encyclopedia
test is a command in Unix
that evaluates conditional expressions.
sh(1)), it is actually a shell builtin, even though external version still exists. In the second form of the command, the [ ] (brackets) must be surrounded by blank spaces. You must test explicitly for file names in the C shell. File-name substitution (globbing) causes the shell script to exit. Functions and operators are treated as separate parameters by the test command. The expression parameter refers to a statement that is checked for a true or false condition.
-e FileName - FileName exists.
Note: All remaining functions 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.
-d FileName - FileName is a directory
.
-f FileName - FileName is a regular file.
-g FileName - FileName's Set Group ID
bit is set.
-h FileName - FileName is a symbolic link
.
-k FileName - FileName's sticky bit is set.
-L FileName - FileName is a symbolic link
.
-p FileName - FileName is a named pipe
(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
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
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 (chdir) into the directory.
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
or hard link
)
, 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 - String1 variable is not a null string.
ically
equal. Any of the following comparisons can be used in place of -eq.
-ne (not equal)
-gt (greater than)
-ge (greater or equal)
-lt (less than)
-le (less or equal)
! - 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 and are therefore not portable. In portable shell scripts, the same effect may be achieved by connecting multiple invocations of test together with the
Exit Status
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 -o ! -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
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...
that evaluates conditional expressions.
Description
The test command evaluates the expression parameter. In some shells (such as FreeBSDFreeBSD
FreeBSD is a free Unix-like operating system descended from AT&T UNIX via BSD UNIX. Although for legal reasons FreeBSD cannot be called “UNIX”, as the direct descendant of BSD UNIX , FreeBSD’s internals and system APIs are UNIX-compliant...
sh(1)), it is actually a shell builtin, even though external version still exists. In the second form of the command, the [ ] (brackets) must be surrounded by blank spaces. You must test explicitly for file names in the C shell. File-name substitution (globbing) causes the shell script to exit. Functions and operators are treated as separate parameters by the test command. The expression parameter refers to a statement that is checked for a true or false condition.
Functions
The following functions are used to construct this parameter:-e FileName - FileName exists.
Note: All remaining functions 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.
-d FileName - FileName is a directory
Directory (file systems)
In computing, a folder, directory, catalog, or drawer, is a virtual container originally derived from an earlier Object-oriented programming concept by the same name within a digital file system, in which groups of computer files and other folders can be kept and organized.A typical file system may...
.
-f FileName - FileName is a regular file.
-g FileName - FileName's Set Group ID
Group identifier (Unix)
In Unix-like systems, multiple users can be categorized into groups. POSIX and conventional Unix file system permissions are organized into three classes, user, group, and others. The use of groups allows additional abilities to be delegated in an organized fashion, such as access to disks,...
bit is set.
-h FileName - FileName is a symbolic link
Symbolic link
In computing, a symbolic link is a special type of file that contains a reference to another file or directory in the form of an absolute or relative path and that affects pathname resolution. Symbolic links were already present by 1978 in mini-computer operating systems from DEC and Data...
.
-k FileName - FileName's sticky bit is set.
-L FileName - FileName is a symbolic link
Symbolic link
In computing, a symbolic link is a special type of file that contains a reference to another file or directory in the form of an absolute or relative path and that affects pathname resolution. Symbolic links were already present by 1978 in mini-computer operating systems from DEC and Data...
.
-p FileName - FileName is a named pipe
Named pipe
In computing, a named pipe is an extension to the traditional pipe concept on Unix and Unix-like systems, and is one of the methods of inter-process communication. The concept is also found in Microsoft Windows, although the semantics differ substantially...
(FIFO).
-r FileName - FileName is readable by the current process.
-s FileName - FileName has a size greater than 0.
-t FileDescriptor - FileDescriptor
File descriptor
In computer programming, a file descriptor is an abstract indicator for accessing a file. The term is generally used in POSIX operating systems...
is open and associated with a terminal
Pseudo terminal
In some operating systems, including Unix, a pseudo terminal is a pseudo-device pair that provides a text terminal interface without an associated device, such as a virtual console, computer terminal or serial port...
.
-u FileName - FileName's Set User ID
User identifier (Unix)
Unix-like operating systems identify users within the kernel by an unsigned integer value called a user identifier, often abbreviated to UID or User ID...
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
Execution (computers)
Execution in computer and software engineering is the process by which a computer or a virtual machine carries out the instructions of a computer program. The instructions in the program trigger sequences of simple actions on the executing machine...
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 (chdir) into the directory.
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
Symbolic link
In computing, a symbolic link is a special type of file that contains a reference to another file or directory in the form of an absolute or relative path and that affects pathname resolution. Symbolic links were already present by 1978 in mini-computer operating systems from DEC and Data...
or hard link
Hard link
In computing, a hard link is a directory entry that associates a name with a file on a file system. . The term is used in file systems which allow multiple hard links to be created for the same file. This has the effect of creating multiple names for the same file, causing an aliasing effect: e.g...
)
String functions
Note that in PerlPerl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...
, 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 - String1 variable is not a null string.
Number functions
Integer1 -eq Integer2 - Integer1 and Integer2 variables are algebraAlgebra
Algebra is the branch of mathematics concerning the study of the rules of operations and relations, and the constructions and concepts arising from them, including terms, polynomials, equations and algebraic structures...
ically
equal. Any of the following comparisons can be used in place of -eq.
-ne (not equal)
-gt (greater than)
-ge (greater or equal)
-lt (less than)
-le (less or equal)
Operators
These functions 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 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.
Examples1. 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 -o ! -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 utilities
- The bash command
- The cshC shellThe C shell is a Unix shell that was created by Bill Joy while a graduate student at University of California, Berkeley in the late 1970s. It has been distributed widely, beginning with the 2BSD release of the BSD Unix system that Joy began distributing in 1978...
command - The findFindIn Unix-like and some other operating systems, find is a command-line utility that searches through one or more directory trees of a file system, locates files based on some user-specified criteria and applies a user-specified action on each matched file...
command - The kshKorn shellThe Korn shell is a Unix shell which was developed by David Korn in the early 1980s and announced at USENIX on July 14, 1983. Other early contributors were AT&T Bell Labs developers Mike Veach, who wrote the emacs code, and Pat Sullivan, who wrote the vi code...
command - The shBourne shellThe Bourne shell, or sh, was the default Unix shell of Unix Version 7 and most Unix-like systems continue to have /bin/sh - which will be the Bourne shell, or a symbolic link or hard link to a compatible shell - even when more modern shells are used by most users.Developed by Stephen Bourne at AT&T...
command - The zshZ shellThe Z shell is a Unix shell that can be used as an interactive login shell and as a powerful command interpreter for shell scripting...
command.