TEST(1) COMMAND REFERENCE TEST(1) NAME test - condition command SYNOPSIS test expr [ expr ] DESCRIPTION Test evaluates the expression expr, and if its value is true then returns zero exit status; otherwise, a nonzero exit status is returned. Test returns a nonzero exit if there are no arguments. If test is executed as the bracket [, the last argument must be the matching bracket ]. The command is available as a builtin sh command and as a shell script for use in csh and make. The following primitives are used to construct expr: -r filename True if the file exists and is readable. -w filename True if the file exists and is writable. -x filename True if the file exists and is executable. -f filename True if the file exists and is not a directory. -d filename True if the file exists and is a directory. -c filename True if the file exists and is a character special file. -b filename True if the file exists and is a block special file. -U filename True if the file exists and is a UTek-domain socket. -u filename True if the file exists and its set-user-ID bit is set. -g filename Printed 4/6/89 1
TEST(1) COMMAND REFERENCE TEST(1) True if the file exists and its set-group-ID bit is set. -k filename True if the file exists and its sticky bit is set (see chmod(2) ). -S filename True if the file exists and is a symbolic link. -s filename True if the file exists and has a size greater than 0. -t [ fildes ] True if the open file whose file descriptor number is fildes (1 by default) is associated with a terminal device. -z s1 True if the length of string s1 is 0. -n s1 True if the length of the string s1 is nonzero. s1 = s2 True if the strings s1 and s2 are equal. s1 != s2 True if the strings s1 and s2 are not equal. s1 True if s1 is not the null string. n1 -eq n2 True if the integers n1 and n2 are algebraically equal. Any of the comparisons -ne, -gt, -ge, -lt, or -le may be used in place of -eq. These primaries may be combined with the following operators: ! Unary negation operator -a Binary and operator -o Binary or operator ( expr ) Parentheses for grouping There are four functions which return integer values and may only be used in algebraic comparisons: -l string Length of string -C filename Time of the last status change to filename (see stat(2)) Printed 4/6/89 2
TEST(1) COMMAND REFERENCE TEST(1) -M filename Time of the last modification to filename (see stat(2)) -A filename Time of the last access to filename (see stat(2)) EXAMPLES The following example shows a piece of a shell script which prints a usage message if the first argument to the script is null: if test -z "$1" then echo "usage : script arg" fi The following shell script prints a message for each of its arguments that are longer than 72 characters: #!/bin/sh for i in "$@" do if [ -l $i -gt 72 ] then echo "Argument too long :" $i fi done This example shows a use for the -M option. If the file copy is older than the file original, the latter is copied to copy: if test -M copy -lt -M original then cp original copy fi RETURN VALUE [0] The expression evaluates to true. [1] The expression evaluates to false. [USAGE] Incorrect command line syntax. Execution terminated. [NP_ERR] An error occurred that was not a system error. Execution terminated. CAVEATS The -a option has higher precedence than -o. Notice that all the operators and flags are separate arguments to test. Printed 4/6/89 3
TEST(1) COMMAND REFERENCE TEST(1) Notice also that parentheses are meaningful to the Shell and must be escaped. The file access operators -r, -w, and -x always evaluate to false if the filename is null, as in the following example: test -r "" Test is actually a shell script which executes the sh builtin command. This is for use in programs such as make(1) that may need to execute test directly. SEE ALSO csh(1csh), expr(1), find(1), make(1), sh(1sh), test(1sh), access(2), chmod(2), and stat(2). Printed 4/6/89 4
%%index%% na:240,76; sy:316,170; de:486,1998;2796,2218;5326,311; ex:5637,937; rv:6574,439; ca:7013,295;7620,604; se:8224,322; %%index%%000000000149