1

i want to write if statment as following :-

if [$x == "string"]

then

echo "ok"

fi

but the following error appear

./bash_if.sh[6]: [0: not found.

Mohammad AL-Rawabdeh
  • 1,612
  • 12
  • 33
  • 54

1 Answers1

7

[ needs a space after it because [ is actually a command (/usr/bin/[ on Linux, though bash has a built-in version that it uses). Without the space, bash converts $x to the value, then tries to run the command [0, just as if you had typed lssomedir or echohi.

Also, if you are testing strings, you should put quotes around $x:

if [ "$x" == "string" ]

Otherwise, if $x is empty or unset, you will get an error because without the quotes, after "expanding" $x, it will look like

if [ == "string" ]
DerfK
  • 19,493
  • 2
  • 38
  • 54