02. Logical Expressions [, test

Beware of the disparities of shell scripting!

We are now about to wade into the murkey waters of logical expressions. Do not try to apply other programming language's syntax or concepts to shell scripting, or you'll be unpleasantly surprised... Let's get started!

To evaluate logical expressions, we use the [ ] brackets. You may think that these brackets are just for syntax, but they're actually a shortcut for the test command.

The test command takes in some evaluation and (depending on the result of the statement) returns some exit code.

Unary and boolean operators

Let's first look at the unary and boolean operators before we learn about specific tests.

Inverting a test

To invert a test, place an exclamation mark as the first argument of the [ command (remember it's not syntax!). For example, the -e operator checks for a file's existence.

# Test if file1 does not exist
[ ! -e file1 ]

Boolean operators

The boolean operators AND and OR can be modeled with the -a and -o flags.

# Test if both file1 and file2 exists
[ -e file1 -a file2 ]


Let's now take a look at some examples to see what some logical expressions evaluate to.

Since a test call does not explicitly return what its exit code is, we need to use the $? command to retrieve the last exit code.

Remember that using the test command is the same as using brackets.

$ test 'hi' = 'hello'; echo $?
$ [ 'hi' = 'hello' ]; echo $?
$ test 'hi' != 'hello'; echo $?
$ [ 'hi' != 'hello' ]; echo $?
$ [ 'hi' === 'hello' ]; echo $?
# === is not a real option, so returns an exit code of 2
-bash: test: ===: binary operator expected

Notice that if an evaluation is true, it returns a 0, but if false, it returns a 1. Any errors result in an error code of 2.

You can see a list of all test options with the man test command, but we'll go through them step by step over the following lessons.

Spacing is important!

Be sure to space out your operators properly - the Command Line is particular picky about this! Joining arguments together (1=3 instead of 1 = 3) can mean errors all over the place, so be cautious!

Handling more than one test with && and ||

If we have more than one command, we can bring them together logically with the && operator or || operators. The evaluation of these boolean operators depend on the exit statuses of the commands.

The shell is designed to be efficient, and so wastes no time running needless commands. For example, if two commands - command1 and command2 are joined with an && operator, and command1 returns an exit status other than 0, command2 is not run.

$ command1 && command2

Similarly, in the case two commands are joined by a || operator, command2 won't be run if command1 already has an exit status of 0.

$ command1 || command2   

We can use these two properties above to create a simple but efficient if/else-like statement.

$ command1 && command2 || command3

command1 is first executed, then command2is also executed if command1 has an exit status of 0. Otherwise, command3 is run.

Hopefully these evaluations make sense - let's now move onto the different types of tests (file, string and arithemetic) which give much more flexibility to control flow.

Take your Linux skills to the next level!

System Admin Handbook

Take your Linux skills to the next level! Try Linux & UNIX

This book approaches system administration in a practical way and is an invaluable reference for both new administrators and experienced professionals. It details best practices for every facet of system administration, including storage management, network design and administration, email, web hosting, scripting, and much more.

$ Check price
74.9974.99Amazon 4.5 logo(142+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Acupressure Mat & Pillow

Aching back from coding all day? Try Back Problems

Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.

$$ Check price
144.87144.87Amazon 4.5 logo(1,890+ reviews)

More Back Problems resources