test command

The test command, also written with single square brackets, that checks a condition such as a file existing or two values being equal, and exits 0 if it holds.

In more detail

Because [ is an ordinary command, its arguments go through word splitting, so an unquoted empty variable vanishes and [ $x = y ] becomes a syntax error. Quote every variable inside single brackets. It is the portable choice for POSIX sh scripts.

Example

if [ -s empty.txt ]; then echo "has data"; fi

Related terms

Taught in

References