Test
Syntax#
- test
- test [!] [ -n | -z ] string
- test [!] { -b | -c | -d | -e | -f | -g | -h | -L | -p | -r | -S | -s | -u | -w | -x } file
- test [!] -t fd
- test [!] string { = | != } string
- test [!] integer { -eq | -ne | -gt | -ge | -lt | -le } integer
- [ ]
- [ [!] [ -n | -z ] string ]
- [ [!] { -b | -c | -d | -e | -f | -g | -h | -L | -p | -r | -S | -s | -u | -w | -x } file ]
- [ [!] -t fd ]
- [ [!] string { = | != } string ]
- [ [!] integer { -eq | -ne | -gt | -ge | -lt | -le } integer ]
Remarks#
If test(1)
is run without any arguments it returns false.
Reference
- Standard
test(1)
- The FreeBSD
test(1)
man-page - The NetBSD
test(1)
man-page - The OpenBSD
test(1)
man-page - The Illumos
test(1)
man-page - The GNU Coreutils online manual section on
test(1)
Multiple Expressions
Though it is an obsoleted part of the XSI standard, many implementations still support multiple expressions with Boolean operators and parenthesis.
The (obsolete) operators are listed below with decreasing precedence.
( expression )
expression -a expression
expression -o expression
Using these (obsolete) operators, a complex shell expression:
if [ "$a" -gt 0 ] && { [ "$b" -ne 2 ] || [ "$b" -e 0 ]; }
then ...
fi
Could be written with one invocation of test(1)
:
if [ "$a" -gt 0 -a '(' "$b" -ne 2 -o "$c" -ne 0 ')' ]
then ...
fi