Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In the process of writing a shell script, I ran into an issue with the following if/else statement that falls somewhere in the middle of the script:

if [ $act -eq "add" ]
    read - "add or update: " $comm
    git commit -m "$comm $file"
    git commit -m "$act $file"

The returning error being:

./gitup: line 13: [: add: integer expression expected

and then proceeds with the rest of the script. How can I have the if segment evaluate/compare the variable to a string input rather than an integer; a different error was required when using "!=" among a couple of other things I tried.

share|improve this question
up vote 12 down vote accepted

Something like this:

if [[ $act = "add" ]]
    echo good
    echo not good

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

share|improve this answer
Thanks. I had tried that, and the script would free, requiring a keystroke, but just found it was caused by an unrelated syntax error further down, so this worked perfectly. Thanks for the help. – tony_perkis666 Jan 30 '13 at 4:57
@josephmarhee: Note that the [[]] test is a Bash specific construct, and this example works just as well with the POSIX [] as used in the question. If the interpreter is explicitly given as #!/bin/bash or similar, the [[]] can be used without issues (and it is a bit faster than the alternative in Bash, I believe - not that it should be a bottle neck anyway), otherwise one should stick with []. If one doesn't need Bash specifics at all, the script will run a bit faster in e.g. Dash. And POSIX ensures inherent portability. – Daniel Andersson Jan 30 '13 at 9:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.