113

My whole script is currently this:

#!/bin/sh   
clear;   
blanko="";   
# Dummy-Variablen
variable=Testvariable;   
if [[$variable == $blanko]];
then   
  echo "Nichts da!"   
else   
  echo $variable   
fi

and if I enter

TestSelect.sh

I get

/usr/bin/TestSelect.sh: line 6: [[Testvariable: command not found   
Testvariable

How can I fix this?

5
  • 17
    Tip for the future: shellcheck will automatically point out this and other basic issues. Commented Nov 1, 2013 at 21:09
  • 2
    Another pointer: you only need a statement-terminating ; if you're putting multiple statements on a single line. Commented Nov 2, 2013 at 2:46
  • 2
    You need spaces between [[ and $variable and $blanko and ]] Commented Jan 20, 2019 at 11:02
  • Other question is about use of [ which is an external utility in Unix but this is different problem Commented Aug 3, 2022 at 8:16
  • Double check that you don't have a non breaking space instead of a regular space. I often typo a nbsp instead of a space and cause this to fail in a hard-to-understand/debug way. Commented Jun 21, 2023 at 9:20

4 Answers 4

246

This is problem:

if [[$variable == $blanko]];

Spaces are required inside square brackets, use it like this:

[[ "$variable" == "$blanko" ]] && echo "Nichts da!" || echo "$variable"
Sign up to request clarification or add additional context in comments.

11 Comments

Note, however, that there's no need to double-quote the variable references (even if the values have embedded spaces).
@mklement0: Just for good practice I always quote them, may not be always needed.
@EpsilonAlpha: English has the same saying: "[I] cannot see the forest for the trees".
but why do we need spaces in the first place like this?
Because bash interprets that as a command in the absence of right syntax
|
40

On a related note, spaces are required around [ ] as well:

if [ "$variable" = "$blanko" ]; then
  # more code here
fi

Note that variables do need to be enclosed in double quotes inside [ ] to prevent word splitting and globbing. Double quotes also help when either of the variables being compared is not set - shell will throw a syntax error otherwise.

Look at the following post to understand why we need spaces around [ ]:

Another related post that talks about other syntax elements that need spaces as well:

Finally, this post talks about the difference between [[ ]] and [ ]:


Related:

Comments

0

Just use #!/bin/bash on tope of script if you are using bash scripting like: if [[ $partition == "/dev/sda2" ]]; then to compare string and run script with ./scriptname.sh or bash scriptname.sh

Comments

-1

If your script runs on your local with /bin/bash but not on your container with sh, then consider adding bash to your container by apk add --no-cache bash.

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.