Tips for using the Linux test command

The [ and test commands are vital conditional statements when scripting.
8 readers like this.
Command line prompt

Opensource.com

The [ 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 [ or test and [[, and there's a technical difference between those commands and your shell's versions of them.

[ vs test commands in Linux

The [ 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 [ or test, you're probably not executing /usr/bin/[ or /usr/bin/test. Instead, you're invoking what's essentially a function of your Bash shell.

You might wonder why [ or test exist in /usr/bin at all. Some shells, such as tcsh, don't have [ and 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 [ or 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
$

The [ and test commands are essentially switches. They emit a true or false response, but considers both of them as success. You can put this to use by pairing the commands with logical operators, such as && and ||. The && operator is executed when a response is true:

$ touch example
$ test -e example && echo "foo"
foo
$ test -e notafile && echo "foo"
$

The || operator executes when a response is false:

$ 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 [ or test to detect a variety of different file types:

  • -f: regular file (returns false for a directory)

  • -d: directory

  • -b: block (such as /dev/sda1)

  • -L or -h: symlink

  • -S: socket

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

Combining tests

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 true:

$ 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 false:

$ touch zombie apocalypse now
$ test -e plant -a -e apocalypse -a -e now && echo "no thanks"
$

The -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

Integer tests

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

Testing testing

The [ and 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.

Seth Kenlon
Seth Kenlon is a UNIX geek, free culture advocate, independent multimedia artist, and D&D nerd. He has worked in the film and computing industry, often at the same time.

2 Comments

Informative article thankyou for it help me so much

Covered most basic information to start with. Thanks

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.