Open-source News

Tips for using the Linux test command

opensource.com - Wed, 10/26/2022 - 15:00
Tips for using the Linux test command Seth Kenlon Wed, 10/26/2022 - 03:00

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.

More Linux resources Linux commands cheat sheet Advanced Linux commands cheat sheet Free online course: RHEL technical overview Linux networking cheat sheet SELinux cheat sheet Linux common commands cheat sheet What are Linux containers? Our latest Linux articles 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 thanksInteger 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"
yesTesting 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.

The [ and test commands are vital conditional statements when scripting.

Image by:

Opensource.com

Linux Command line What to read next This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License. Register or Login to post a comment.

Customer success stories: How Red Hat’s products and services provide solutions for financial, non-profit and professional certification organizations

Red Hat News - Wed, 10/26/2022 - 12:00

In this month’s customer success highlights, learn how FIWARE, Electrical Training Alliance and SVA employed several Red Hat products to help build eco-smart city solutions, develop reusable microservices-based applications and construct a robust modern platform that meets strict compliance requirements.

Maximizing the value of the technology you have

Red Hat News - Wed, 10/26/2022 - 12:00

It’s easy for businesses to go into survival mode right now. Supply chains are reeling from inflationary and workforce pressures; consumer demand is bracing itself for the full impacts of the cost of living crisis; and climate change is no longer something happening elsewhere, all while the machinations of geopolitics are spooking markets on an almost daily basis. We live in uncertain and unpredictable times.

Oracle Releases GraalVM 22.3, GraalVM CE Java Code Going To OpenJDK

Phoronix - Wed, 10/26/2022 - 07:20
GraalVM 22.3 is now available as Oracle's quarterly feature release to this high performance Java JVM/JDK that also supports additional programming languages and execution models...

Python 3.11 Performance Benchmarks Show Huge Improvement

Phoronix - Wed, 10/26/2022 - 04:00
While this summer I ran some early Python 3.11 benchmarks using the development state at the time, given yesterday's Python 3.11 release I ran some fresh performance tests of the official Python 3.11 version against prior Python 3 releases.

Lennart Poettering Talks Up A "Brave New Trusted Boot World" For Linux

Phoronix - Wed, 10/26/2022 - 03:00
Systemd lead developer Lennart Poettering has written a lengthy blog post entitled a "brave new trusted boot world" in which he outlines current issues with the Linux boot process and how there is a trajectory for providing the Linux boot experience with more robustness, simplicity, and trust...

Chrome 107 Released With HEVC Hardware Decoding, Other Additions

Phoronix - Wed, 10/26/2022 - 02:37
Google today promoted the Chrome 107 web browser to their stable channel across all supported platforms...

OpenZFS Eyes Faster Scrub, Improved Compression, uZFS, Better Performance

Phoronix - Wed, 10/26/2022 - 02:00
Taking place yesterday and today in San Francisco has been the annual OpenZFS Developer Summit. Talks this year ranged from how Amazon AWS is making use of OpenZFS to a number of optimizations and improvements currently being tackled by open-source developers...

Pages