Getting “command not found” error while comparing two strings in Bash

后端 未结 4 1794
失恋的感觉
失恋的感觉 2020-11-22 12:19

My whole Script is currently this:

#!/bin/sh   
clear;   
blanko=\"\";   
# Dummy-Variablen
variable=Testvariable;   
if [[$variable == $blanko]];
then   
           


        
4条回答
  •  一个人的身影
    2020-11-22 13:01

    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 [ ]:

    • Why should there be a space after '[' and before ']' in Bash?

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

    • Why is whitespace sometimes needed around metacharacters?

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

    • Difference between single and double square brackets in Bash

    Related:

    • “0: command not found” in Bash

提交回复
热议问题