Problem
I have to compare a string "problem1.sh" with itself. It works fine in case of solution1.sh
(given below) where I have used square brackets for comparison. However, it does not work in case of solution2.sh
(given below) where I have used round brackets. It shows the mentioned error (given below).
What I have tried?
I have tried to learn the difference between the use of square and round brackets in bash script from here and here. I understand that ((expression))
is used for comparison arithmetic values, not for the string values.
So, what does create the problem?
If I remove sub-string ".sh" from the string "problem1.sh" and compare using the same statement if (("problem1" == "problem1"))
, it works fine. However, when I just add "." in the string, it creates the problem. Even if I remove everything except "." from the string and use the statement if (("." == "."))
, it shows me error.
Then, my question
If the statement if (("problem1" == "problem1"))
can work fine (may be, it will work fine for every letter of English alphabet), why does "." string create the problem? I mean, Why we can not compare "." using round brackets in if statement of bash script (e.g if (("." == "."))
) when we can compare letters using the same expression (e.g if (("findError" == "findError"))
) ?
solution1.sh
if [ "problem1.sh" == "problem1.sh" ]
then
printf "Okay"
fi
solution2.sh
if (( "problem1.sh" == "problem1.sh" ))
then
printf "Okay"
fi
Error Message For solution2.sh
./solution2.sh: line 1: ((: problem1.sh == problem1.sh : syntax error:
invalid arithmetic operator (error token is ".sh == problem1.sh ")
((...))
)? What is it you want to actually do?[[ ]]
?(( ))
is for artihmetic operations so why do you need that one? You can even do arithmetic operations inside of[ ]
or[[ ]]
if you use-eq
,lt
,gt
,lte
, orgte
.((...))
is for arithmetic evaluation, then why are you trying to compare strings using this? In your exampleproblem1
will be taken as a variable's name. If that variable is unset, the value used will be 0 (or possibly empty, one or the other, integer evaluation is weird). When you add.sh
you introduce nonsense into the expression (0.sh
(or.sh
) is not a valid integer expression). I still don't know what it is you are trying to do.