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.

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

How Linux Works

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

In this completely revised second edition of the perennial best seller How Linux Works, author Brian Ward makes the concepts behind Linux internals accessible to anyone curious about the inner workings of the operating system. Inside, you'll find the kind of knowledge that normally comes from years of experience doing things the hard way.

$ Check price
39.9539.95Amazon 5 logo(114+ reviews)

More Linux & UNIX resources