Logical Operations
Explore how to apply logical operations and arithmetic evaluation in Bash script conditions. Understand the use of comparison symbols, the (( operator, logical AND, OR, and NOT operators, and differences between arithmetic evaluation and expansion to write more efficient and accurate shell scripts.
We'll cover the following...
Comparison symbols
The [[ operator is inconvenient for comparing integers in the if statement. This operator uses two-letter abbreviations for expressing the relations between numbers. For example, the -gt abbreviation means greater. When we apply the (( operator instead, we can use the usual comparison symbols instead. These symbols are >, <, and =.
Here is an example. Let’s suppose that we want to compare a variable with the number 5. The following if construction does so:
if ((var < 5))
then
echo "The variable is less than 5"
fi
We can run the codes discussed in this lesson by modifying the
script.sh. Click on the “Run” button and then execute the file.
#!/bin/bash var="$1" if ((var < 5)) then echo "The variable is less than 5" fi
The construction uses the (( operator in ...