sh

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

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

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow