In the Linux Bash scripting, logical operations are used to combine conditional statements and perform actions based on their outcomes. Learning how Logical operations can be performed in Bash enahnces our ability to write efficient and powerful scripts.

In Bash scripting, boolean values are represented as either true or false. However, the shell doesn’t natively support boolean types. Instead, integer values are used to represent boolean logic.

Logical Operators

There are three main logical operators available in Bash scripting:

  • AND Operator (&&): Denoted by &&, the AND operator evaluates two conditions and returns true only if both conditions are true.

Example:

1age=19
2gender="male"
3if [ $age -gt 18 ] && [ $gender == "male" ]; then
4    echo "You are a male adult."
5fi
  • OR Operator (||): Denoted by ||, the OR operator evaluates two conditions and returns true if either of the conditions is true.

Example:

1country="India"
2if [ $country == "USA" ] || [ $country == "Canada" ]; then
3    echo "You are in North America."
4fi
  • NOT Operator (!): Denoted by !, the NOT operator negates a condition. If a condition is true, applying the NOT operator will make it false, and vice versa.

Example:

1country="UK"
2if ! [ $country == "India" ]; then
3    echo "This is not India."
4fi

Below is a common script explaining different operations.

 1#!/bin/bash
 2echo I will succeed
 3echo $?
 4# The same kind of status codes exist for true and false
 5# true has status code of 0
 6# Based on these status codes it will decide further action
 7# Bash supports logical operations like && and ||
 8true # This has exit code 0
 9echo "Status code after true $?"
10
11false # This has exit code 1
12echo "Status code after false $?"

Logical operators can be combined to create more complex expressions. Parentheses can be used to group conditions and control their precedence.

Example:

1if [ $age -gt 18 ] && ( [ $gender == "male" ] || [ $gender == "female" ] ); then
2    echo "You are an adult."
3fi

Short-circuit operations

Something that’s very common in shell scripting is to use these operations to short-circuit execution of commands. For example, if we want second command to run only when first command has failed, in that case, we can use ||. This OR operation evaluates second expression only if first one fails. Similarly, if we want both expression to run only if first expression is true, then we can use &&. In this case, if first expression results in false then second expression is not evaluated at all. Below script demonstrates examples.

1echo Hello && echo Hi
2echo Hi || echo Hello

This pattern can be used for short-circuit evaluations

1cp test.txt another.txt && echo "Copied script text.txt" || echo "Copy failed"

In above example snippet, if the copy succeeds, it produces output “Copied script test.txt” but if it fails, it produces output “Copy failed”. You may notice this kind of short-circuit pattern in some of the bash scripts.

Logical operations in Linux Bash terminal provide a powerful way to evaluate conditions and control the flow of your scripts. By utilizing these logical operators effectively, you can make flexible shell scripts.