Boolean Operators

Bash Scripting Boolean Operator Examples

Logical Boolean Operators

So far we have seen some simple tests with the "if" statement. To add further flexibility to our if statements we can incorporate some logical operators. These operators are the "!" exclamation mark which is used for logical negation, "&&" double ampersand which is our logical AND and our logical OR "||" two vertical pipes.

! Logical Negation

We can reverse our test within an "if" statement with a "!" NOT operator:
Example of ! Logical Negation


if [ ! -f /home/john/scripts/myfile.txt ]; then
   echo "File NOT found"

In the above example we are negating a check for a file presence. If the file is not found then our outcome is True. In this example if the file was not found, we would execute the echo statement.

&& Logical AND

The Logical AND "&&" is a boolean operator that executes following commands based on the outcome of previously executed commands. If the outcome of the previous command is "0" True, then execute the following command. The basic syntax is:

command1 && command2

If "command1" successfully executes with an exit status of "0" True, then run "command2". This functionality is very useful within scripts.

Examples of Logical AND &&

grep "john" /etc/passwd && echo "john found"

The above command searches for a user called "john". If the user is found, then the echo statement is executed.

Output from the above command

john@john-desktop:~/scripts$ grep "john" /etc/passwd && echo "john found"
john found

Example of a Logical AND within an "if" statement



if [[ $john == "21" && $jessie == "9" ]]; then
   echo "John is $john and Jessie is $jessie"

if [[ $john == "21" && $jessie == "10" ]]; then
   echo "John is $john and Jessie is $jessie"

Output from the above script

john@john-desktop:~/scripts$ ./ 
John is 21 and Jessie is 9	

In the above example for our if statement to be true, the variable john has to have a value of "21" and the variable jessie has to have a value of "9". Only the first "if" statement ran its associated echo command. The second "if" statement was not true because it was looking for a value of "21" and "10". As only one of these values matched, the if statement is false.

|| Logical OR

The Logical OR "|| is an operator that will execute other commands based on the exit status of another command.

The basic syntax of a Logical OR is:

command1 || command2

command2 is only executed if command1 returns a none zero exit code. Simply this means run command1 successfully otherwise run command2. We can illustrate this with the following example:

Example Logical OR ||

john@john-desktop:~/scripts$ grep "bill" /etc/passwd || echo "Failed"
john@john-desktop:~/scripts$ grep "john" /etc/passwd || echo "Failed"

In the above example we used the grep command to search for a user within the "/etc/passwd" file. In the first example we looked for a user called "bill". As the user "bill" does not exist on this system our echo command is executed. In the second example we use a username that exists on our system. This time our grep command successfully finds the user. The associated echo command is not executed as the first command had a "0" exit code.

Combine Logical Operators

Multiple Logical operators can be combined together. Below is a simple example of using multiple operators.:

cat /etc/shadow > /dev/null && echo "File opened successfully" || echo "Failed to open File"

In the above example we are attempting to open the "/etc/shadow" file. If we are successful then the echo statement "File opened successfully" will be issued. If we were unable to open the file, then the echo statement "failed to open file" would be displayed. (> /dev/null is used to throw away any output)

As only the root user can normally open the "/etc/shadow" file, we can demonstrate the above command easily:

Output from above command run as root:

john@john-desktop:~/scripts$ sudo cat /etc/shadow > /dev/null && echo "File opened successfully" || echo "Failed to open File"
File opened successfully

Output from above command run as a normal user:

john@john-desktop:~/scripts$ cat /etc/shadow > /dev/null && echo "File opened successfully" || echo "Failed to open File"
cat: /etc/shadow: Permission denied
Failed to open File