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

Margaret
  • 5,749
  • 20
  • 56
  • 72
  • 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) – plundra Dec 17 '10 at 08: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. – Ben Jackson Dec 17 '10 at 08:43
  • @plundra: Those *are* integers. See my comment on RageZ's answer. – Dennis Williamson Dec 17 '10 at 10:20
  • possible duplicate of [bash, command not found](http://stackoverflow.com/questions/16694586/bash-command-not-found) – hyde Mar 21 '14 at 07:46

3 Answers3

39

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
SiegeX
  • 135,741
  • 24
  • 144
  • 154
  • Can you detail on that change, in what context is that a valid statement ? Which shells ? – tvCa Nov 30 '14 at 13:06
  • @tvCa which statement are you referring to, the requirement for a space after `[` or `(( ))` ? – SiegeX Dec 01 '14 at 16:23
  • Yes ... I mean about using (( )) instead of [ ] - From what I read, the replacement for [ ] is [[ ]] - and not (( )) – tvCa Dec 01 '14 at 20:12
  • @tvCa In bash `(( ))` is used for arithmetic evaluation which is exactly what the OP wants to do. – SiegeX Dec 10 '14 at 19:42
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.

RageZ
  • 26,800
  • 12
  • 67
  • 76
  • 4
    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. – Dennis Williamson Dec 17 '10 at 10:19
  • 6
    Spaces! I knew it was going to be something small. And people complain about *python's* use of significant whitespace. :S – Margaret Dec 17 '10 at 10:55
  • @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. – Charles Duffy Apr 01 '20 at 22:39
9

Try adding spaces around your brackets:

if [ "1" -eq "2" ]
Michael Burr
  • 333,147
  • 50
  • 533
  • 760