The test, [, and [[ commands

The test builtin command is used to evaluate conditional expressions; it tests whether two strings are equal, whether a file exists, whether a string is empty, and other functions. The complete list of tests is available with help test.

For example, to test whether a file exists, you might use the following:

$ test -e /etc/passwd && echo 'Password file exists!'
Password file exists!

There is an alternative way to write this command, called [. No, that's not a typo; [ is the name of the command! We can see this with type; there's even a program on our filesystem by the same name:

bash$ type -a [
[ is a shell builtin
[ is /usr/bin/[

The primary difference between test and [ is that [ requires its last argument to ...

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.