18

Ok, this is probably going to be ultra obvious to anyone that has spent more time with bash than I have.

I'm trying to run this code:

#!/bin/bash

if ["1" -eq "2"] 
then
    echo "True"
else
    echo "False"
fi

but when I execute the file, it sends back

./test.sh: line 3: 1: command not found
False

There must be something major I'm missing. I've seen people use a semicolon after the brackets, this doesn't seem to make any difference... :S

4
  • 2
    [ is a program, try which [ and you'll see. And you can't execute a program with arguments written without a whitespace... (And as others have answered, -eq is for integers only. Which it will tell you when using [ correct) Commented Dec 17, 2010 at 8:43
  • 1
    Everyone already said that you need a space after [. The reason is that [ is a command -- it's an alias for test. These days it's a shell builtin but you can find /bin/[ on most systems. Commented Dec 17, 2010 at 8:43
  • @plundra: Those are integers. See my comment on RageZ's answer. Commented Dec 17, 2010 at 10:20
  • possible duplicate of bash, command not found Commented Mar 21, 2014 at 7:46

3 Answers 3

40

You need to add a space after the [ and before the ] like so:

if [ "1" -eq "2" ]

However, that way is deprecated and the better method to use is:

#!/bin/bash

if ((1 == 2)) 
then
    echo "True"
else
    echo "False"
fi
Sign up to request clarification or add additional context in comments.

4 Comments

Can you detail on that change, in what context is that a valid statement ? Which shells ?
@tvCa which statement are you referring to, the requirement for a space after [ or (( )) ?
Yes ... I mean about using (( )) instead of [ ] - From what I read, the replacement for [ ] is [[ ]] - and not (( ))
@tvCa In bash (( )) is used for arithmetic evaluation which is exactly what the OP wants to do.
11

yep eq is used only for arithmetic comparaisons.

for string comparison you have to use =

#!/bin/bash

if [ "1" = "2" ] 
then
    echo "True"
else
    echo "False"
fi

plus you need some space around the brackets.

3 Comments

Numbers are strings in the shell. The quotes don't change the fact that they're numbers. [ 10 -gt 2 ] is true. Comparisons, on the other hand, can be for strings or numbers. One example, you've shown. Others that are specific to shells that support [[: string: [[ "10" > "2" ]] (false) or [[ "10" -gt "2" ]] (true). Again, it's not the quotes that make the difference.
Spaces! I knew it was going to be something small. And people complain about python's use of significant whitespace. :S
@Margaret Heh. The thing shell does that's weird here is having [ not be syntax, but instead be a regular command (as far as the parser is concerned; there's actually a built-in implementation so /bin/[ and /usr/bin/[ aren't used by common shells, but that's just a performance optimization). It's completely normal to need a space between the name of a command and its arguments.
11

Try adding spaces around your brackets:

if [ "1" -eq "2" ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.