Issue
I have written the following bash script:
if [ "crack" == "crack" -a "something/play" == *"play"* ];
then
echo "Passed"
else
echo "Failed"
fi
However the right side of this comparison is not working.
I noticed that if I use it the only right side with [[ "something/play" == *"play"* ]]
it works correctly but how do I combine the two conditions inside the if clause.
Solution
It's a difference between [
and [[
. The first is a standard command, where =
just tests for equality. (Note that the standard operator is =
, not ==
.) The latter is a feature from ksh, supported in Bash and Zsh, and there, =
/==
is a pattern match. Also, you should avoid using -a
within [ .. ]
, it can break if you do something like [ "$a" = foo -a "$b" = bar ]
and $a
or $b
contains a !
.
So,
$ if [[ "crack" == "crack" && "something/play" == *"play"* ]]; then echo true; fi
true
See also (in unix.SE): Why is [
a shell builtin and [[
a shell keyword? and What is the difference between the Bash operators [[ vs [ vs ( vs ((?.
Answered By - ilkkachu Answer Checked By - Marilyn (WPSolving Volunteer)