The Computer Oracle

How can I compare a variable to a text string, rather than integer, in an if/else statement?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Unforgiving Himalayas Looping

--

Chapters
00:00 How Can I Compare A Variable To A Text String, Rather Than Integer, In An If/Else Statement?
00:39 Accepted Answer Score 62
00:58 Answer 2 Score 10
01:13 Thank you

--

Full question
https://superuser.com/questions/543793/h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#bash #shellscript #bashscripting

#avk47



ACCEPTED ANSWER

Score 62


Something like this:

act="add"
if [[ $act = "add" ]]
then
    echo good
else
    echo not good
fi

-eq is for number comparison, use = for string comparison




ANSWER 2

Score 10


This method would also work. Very similar to @Guru's answer but removes the need for double square brackets.

if [ "$act" == "add" ]
then
echo "Good!"
      else
      echo "Not good!"
fi