test - Evaluate expression
Synopsis
test expression
[ expression ]
Description
The test utility checks the type of a file,
checks permissions on files, compares two strings, or compares two
arithmetic expressions.
The test utility tests conditions for files using
the following primaries:
- -b file
- True if file exists and is a block special file.
- -c file
- True if file exists and is a character special file.
- -d file
- True if file exists and is a directory.
- -e file
- True if file exists regardless of type.
- -f file
- True if file exists and is a regular file.
- -g file
- True if file exists and its set group id flag is set.
-
-G file
- True if file exists and is owned by the effective group id.
- -h file
- True if file exists and is a symbolic link.
- -k file
- True if file exists and its restricted deletion flag is set.
- -L file
- True if file exists and is a symbolic link.
- -N file
- True if file exists and is a native object.
-
-O file
- True if file exists and is owned by the effective user id.
- -p file
- True if file exists and is a pipe.
- -r file
- True if file exists and is readable.
- -s file
- True if file exists and has a size greater than zero.
- -S file
- True if file exists and is a socket.
- -u file
- True if file exists and its set user id flag is set.
- -w file
- True if file exists and is writable.
- -x file
- True if file exists
and is executable. This only means that the execute bit is on. If
file is a directory, the directory can be searched.
-
file1 -ef file2
- True if file1 and file2 are different names for the same
file (they have the same device and inode numbers).
- file1 -nt file2
- True if file1 is newer than file2 or file2 does not exist.
- file1 -ot file2
- True if file1 is older than file2 or file2 does not exist.
The test utility tests conditions for checking status
using the following primaries:
-
-o optname
- True if shell option optname is enabled.
- -t fd
- True if file descriptor fd is open and associated with a terminal.
The test utility tests conditions for comparing strings
using the following primaries:
- -n string
- True if the length of string is non-zero.
- -z string
- True if the length of string is zero.
- string
- True if string is not the null string.
- string1 = string2
- True if the strings are identical.
-
string1 == string2
- True if the strings are identical.
- string1 != string2
- True if the strings are not identical.
-
string1 < string2
- True if string1 sorts before string2 in the collation
sequence of the current locale.
- string1 > string2
- True if string1 sorts after string2 in the collation
sequence of the current locale.
The test utility tests conditions for comparing
arithmetic expressions using
the following primaries:
- exp1 -eq exp2
- True if the arithmetic expressions are equal.
- exp1 -ne exp2
- True if the arithmetic expressions are not equal.
- exp1 -gt exp2
- True if the first arithmetic expression is greater than the second arithmetic expression.
- exp1 -ge exp2
- True if the first arithmetic expression is greater than or equal to the second arithmetic expression.
- exp1 -lt exp2
- True if the first arithmetic expression is less than the second arithmetic expression.
- exp1 -le exp2
- True if the first arithmetic expression is less than or equal to the second arithmetic expression.
The above primaries can be combined to form complex expressions
using the following operators:
- ! expr True if expr is
false.
- expr1 -a expr2 True if both
expressions are true.
- expr1 & expr2 True if
both expressions are true.
-
expr1 && expr2 True if
both expressions are true.
- expr1 -o expr2 True if
either expression is true.
- expr1 | expr2 True if either
expression is true.
-
expr1 || expr2 True if either
expression is true.
- (expr) Parentheses
are for grouping.
The -a, &, and && operators
have higher precedence than the -o,
| operators, and || operators.
Options
See above.
Operands
All operators and flags are separate arguments.
Exit Status
- 0 when expression is true.
- 1 when expression is false.
- >1 when there is an error.
Examples
- See if /home is a directory:
test -d /home
- See if one integer is less than or equal to another:
test "$index" -le "$count"
- See if two strings are equal:
test "$REPLY" = "Yes"