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 takes in some evaluation and (depending on the result of the statement) returns some exit code.
Let's first look at the unary and boolean operators before we learn about specific tests.
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 ]
The boolean operators AND and OR can be modeled with the
# 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.
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 $? 1 $ [ 'hi' = 'hello' ]; echo $? 1 $ test 'hi' != 'hello'; echo $? 0 $ [ 'hi' != 'hello' ]; echo $? 0 $ [ 'hi' === 'hello' ]; echo $? # === is not a real option, so returns an exit code of 2 -bash: test: ===: binary operator expected 2
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
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.
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!
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 -
command2 are joined with an && operator, and
command1 returns an exit status other than
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
$ 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
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.
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
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