The [[ Operator
Learn how to compare two strings using the [[ operator.
We'll cover the following
When to use the [[ operator
We’ve gotten acquainted with if
statements. They call a Bash built-in command or a utility in a specific condition.
Let’s consider the options we have to make an if
condition. Let’s suppose we want to check if a text file contains a phrase. When the phrase is present, we print a message in the log file.
We can combine the if
statement and the grep
utility to solve the task. Let’s put the grep
call in the if
condition. If the utility succeeds, it returns zero exit status. In this case, the if
condition equals true
and Bash prints the message.
The grep
utility prints its result to the output stream. We do not need it when calling grep
in the if
condition. We can get rid of the utility’s output using the -q
option. Then, we get the following if
statement:
if grep -q -R "General Public License" /usr/share/doc/bash
then
echo "Bash has the GPL license"
fi
Let’s click on the “Run” button and then run the script using
./script.sh
.
Get hands-on with 1200+ tech skills courses.