Server Fault is a question and answer site for system and network administrators. 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

i want to write if statment as following :-

if [$x == "string"]


echo "ok"


but the following error appear

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

share|improve this question
I don't see how a question about bash scripting would be off-topic here. – mfinni Dec 30 '10 at 22:40
up vote 7 down vote accepted

[ 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" ]
share|improve this answer

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.