[ command, often called a "test," is a command from the GNU Core Utils package, and initiates a conditional statement in Bash. Its function is exactly the same as the
test command. When you want to execute a command only when something is either true or false, use the
[ or the
test command. However, there's a significant difference between
[[, and there's a technical difference between those commands and your shell's versions of them.
[ vs test commands in Linux
[ and the
test commands, installed by the GNU Core Utils package, perform the same function using a slightly different syntax. (You might find it difficult to search for documentation using the single left-square bracket character, however, so many users find
test easier to reference.) Bash and similar shells happen to also have the
[ and the
test commands built-in, and the built-in versions supersede the ones installed in
/usr/bin. In other words, when you use
test, you're probably not executing
/usr/bin/test. Instead, you're invoking what's essentially a function of your Bash shell.
You might wonder why
test exist in
/usr/bin at all. Some shells, such as tcsh, don't have
test built-in, so if you want to use those commands in that shell, you must have them installed as separate binaries.
The bottom line is that as long as you don't get an error when you type a command starting with
test, then you've got everything you need. It almost never matters whether your shell or your
bin directory is providing the commands.
Testing for a file
It's common to want to know whether a file exists, often so you can confidently proceed with some action, or so you can avoid "clobbering" it with a file of the same name. In an interactive shell session, you can just look to see whether the file exists but in a shell script, you need the computer to determine that for itself. The
-e option tests whether a file exists, but its apparent response is the same either way.
$ touch example $ test -e example $ test -e notafile $
test commands are essentially switches. They emit a
false response, but considers both of them as success. You can put this to use by pairing the commands with logical operators, such as
&& operator is executed when a response is
$ touch example $ test -e example && echo "foo" foo $ test -e notafile && echo "foo" $
|| operator executes when a response is
$ touch example $ test -e example || echo "foo" $ test -e notafile || echo "foo" foo $
If you prefer, you can use square brackets instead of
test. In all cases, the results are the same:
$ touch example $ [ -e example ] && echo "foo" foo $ [ -e notafile ] && echo "foo" $
Testing for file types
Everything in Linux is a file, so when you can test for the existence of a directory with the
-e option, the same way you test for a file. However, there are different kinds of files, and sometimes that matters. You can use
test to detect a variety of different file types:
-f: regular file (returns
falsefor a directory)
-b: block (such as
There are more, but those tend to be the most common.
Testing for file attributes
You can also look at metadata of a file:
-s: a file with the size greater than zero
-N: a file that's been modified since it was last read
You can test by ownership:
-O: a file owned by the current primary user
-G: a file owned by the current primary group
Or you can test by permissions (or file mode):
-r: a file with read permission granted
-w: a file with write permission granted
-x: a file with execute permission granted
-k: a file with the sticky bit set
You don't always just have to test for a single attribute. The
-a option ("and") allows you to string several tests together, with the requirement that all tests return as
$ touch zombie apocalypse now $ test -e zombie -a -e apocalypse -a -e now && echo "no thanks" no thanks
If any expression fails, then the test returns
$ touch zombie apocalypse now $ test -e plant -a -e apocalypse -a -e now && echo "no thanks" $
-o option ("or") requires that one expression is true:
$ touch zombie apocalypse now $ test -e zombie -o -e plant -o -e apocalypse && echo "no thanks" no thanks
You can also test integers. That's not necessarily directly useful (you probably inherently know that 0 is less than 1, for instance) but it's invaluable when you're using variables in a script.
The operators are fairly intuitive once you understand the schema:
-eq: equal to
-ne: not equal
-ge: greater than or equal to
-gt: greater than
-le: less than or equal to
-lt: less than
Here's a simple example:
$ nil=0 $ foo=1 $ test $foo -eq $nil || echo "Those are not equal." Those are not equal. $ test $foo -eq 1 && echo "Those are equal."
Of course, you can combine tests.
$ touch example $ test $foo -ne $nil -a -e example -o -e notafile && echo "yes" yes
test commands are vital conditional statements when scripting. These are easy and common ways to control the flow of your code. There are yet more tests available than what I've covered in this article, so whether you used Bash, tcsh, ksh, or some other shell entirely, take a look at the man page to get the full spectrum of what these commands offer.