The Computer Oracle

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

-------------------------------------------------------------------------------
Become or hire the top 3% of the developers on Toptal https://topt.al/25cXVn
-------------------------------------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puddle Jumping Looping

--

Chapters
00:00 Question
00:52 Accepted answer (Score 62)
01:10 Answer 2 (Score 10)
01:28 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