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?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

Command Line Kung Fu

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

Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.

$ Check price
14.9914.99Amazon 4.5 logo(27+ reviews)

More Linux & UNIX resources