Bash AND Logical Operator
In Bash, the logical AND operator && is used when two conditions or commands must both succeed. In a conditional expression, the combined result is true only when both operands are true.
You can use Bash AND with if statements, while loops, [[ ... ]] conditional expressions, arithmetic conditions, and command lists. Bash also uses short-circuit evaluation: when the left side of && is false or a command fails, Bash does not need to evaluate or run the right side.
Syntax of Bash AND Operator with &&
Following is the syntax of AND logical operator in Bash scripting.
operand_1 && operand_2
where operand_1 and operand_2 are logical expressions or boolean conditions that return either true or false. && is the operator used for AND boolean operation. It is read as double ampersand.
When && joins commands, the command on the right is executed only if the command on the left finishes successfully with an exit status of 0.
Bash AND Operator Truth Table
Following truth table gives information about the returned value by AND logical operator for different valid operand values.
| Operand_1 | Operand_2 | Operand_1 && Operand_2 |
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
The important rule is that the result is true only in the first row, where both operands are true.
Bash AND Operator in IF condition
In the following example, we shall use Bash AND logical operator, to form a compound boolean expression for Bash IF.
We shall check if the number is even and also divisible by 10.
Bash Script File
#!/bin/bash
num=50
if [ $((num % 2)) == 0 ] && [ $((num % 10)) == 0 ];
then
echo "$num is even and also divisible 10."
fi
Output
50 is even and also divisible 10.
The first test checks whether the remainder after division by 2 is zero. The second test checks whether the remainder after division by 10 is zero. Because both conditions are true for 50, the body of the if statement is executed.
Using Bash && Inside [[ … ]] Conditions
Bash also supports && directly inside the [[ ... ]] conditional construct. This form keeps related conditions inside one compound test.
#!/bin/bash
age=25
country="India"
if [[ $age -ge 18 && $country == "India" ]]; then
echo "Both conditions are true."
fi
$age -ge 18 checks that age is greater than or equal to 18, while $country == "India" compares the string value. The message is printed only when both tests succeed.
Both conditions are true.
Bash AND Operator for Numeric Comparisons
For numeric tests, Bash comparison operators such as -gt, -ge, -lt, -le, -eq, and -ne can be combined with &&.
The following condition checks whether a number is greater than 10 and less than 20.
#!/bin/bash
num=15
if [[ $num -gt 10 && $num -lt 20 ]]; then
echo "$num is between 10 and 20."
fi
15 is between 10 and 20.
Bash AND Operator for String Comparisons
You can combine string conditions in the same way. For example, the following script checks that a username is not empty and has the expected value.
#!/bin/bash
username="admin"
if [[ -n $username && $username == "admin" ]]; then
echo "Administrator account found."
fi
-n $username is true when the string is not empty. The second condition checks whether its value is admin. Both must be true for the message to be printed.
Bash AND Operator with Arithmetic (( … )) Conditions
Inside Bash arithmetic evaluation with (( ... )), you can use familiar arithmetic comparison operators together with &&.
#!/bin/bash
num=24
if (( num > 0 && num % 2 == 0 )); then
echo "$num is a positive even number."
fi
24 is a positive even number.
This form is useful when the complete condition is arithmetic. The first expression verifies that the number is positive, and the second verifies that it is even.
Bash && Between Commands
The && operator can also connect commands. In this form, the second command runs only when the first command succeeds.
command1 && command2
For example, a directory can be created and entered only if its creation succeeds.
mkdir project && cd project
If mkdir project returns a successful status, Bash runs cd project. If the first command fails, the second command is skipped. This behavior is commonly called short-circuit evaluation.
Chaining Multiple Bash Commands with &&
More than two commands can be connected with &&. Bash proceeds from left to right and stops the AND chain when one command fails.
mkdir reports && cd reports && touch summary.txt
Here, cd reports is attempted only after the directory is created successfully, and touch summary.txt is attempted only after changing into that directory successfully.
Bash AND Operator in While Loop Expression
In this example, we shall use Bash AND boolean logical operator in while expression.
Bash Script File
#!/bin/bash
a=0
b=0
a_max=10
b_max=5
# and opertor used to form a compund expression
while [[ $a -lt $a_max+1 && $b -lt $b_max+1 ]]; do
echo "$a"
let a++
let b++
done
Output
0
1
2
3
4
5
The loop continues only while both comparisons remain true. Although a can continue beyond 5, the condition involving b becomes false after the displayed values, so the combined AND condition becomes false and the loop ends.
Combining Three or More Bash AND Conditions
You can use multiple && operators when every condition must be satisfied. The following example requires a number to be positive, even, and less than 100.
#!/bin/bash
num=40
if [[ $num -gt 0 && $((num % 2)) -eq 0 && $num -lt 100 ]]; then
echo "All three conditions are true."
fi
All three conditions are true.
If any one of these conditions is false, the complete AND expression is false.
Difference Between Bash && and &
&& and & have different purposes in Bash. Do not use a single ampersand when you intend to perform a logical AND operation.
- && connects conditions or commands so that the right side depends on the success or truth of the left side.
- & placed after a command runs that command asynchronously in the background.
command1 && command2
command1 &
The first line forms an AND command list. The second line starts command1 as a background job.
Bash && with [ … ] Compared with [[ … ]]
When using the traditional [ ... ] test command, separate tests can be joined by placing && between them.
if [ condition1 ] && [ condition2 ]; then
commands
fi
With Bash’s [[ ... ]] conditional syntax, the AND operation can be written inside the same conditional expression.
if [[ condition1 && condition2 ]]; then
commands
fi
Both forms are useful in Bash scripts. When writing Bash-specific compound conditions, [[ ... ]] provides a convenient way to keep the logical expression together.
Common Bash AND Operator Mistakes
- Do not use a single
&when you mean logical AND. A single ampersand is related to background execution. - Do not assume the command after
&&always runs. It runs only if the command before it succeeds. - When using
[ ... ], keep each test properly closed before joining separate tests with&&. - Use numeric comparison operators such as
-eq,-lt, and-gtin test expressions when comparing integers. - For arithmetic expressions inside
(( ... )), normal arithmetic operators such as==,<, and>can be used.
Bash AND Operator Summary
In this Bash Tutorial, we learned the syntax of Bash AND logical operator and how to use it in preparing compound expression for conditional statements or looping statements.
The core rule is that condition1 && condition2 is true only when both conditions are true. For command lists, command1 && command2 runs the second command only when the first command succeeds. The same operator can therefore express logical requirements in tests and conditional command execution in Bash scripts.
TutorialKart.com