Pergunta

I'm studyng Bash, and I see that the form

C=example

echo "$C"

give the same result of the form

C="example"

echo $C

I'd like to know if is better put the " " in the assignment of the variable or after the $. Or if it is indifferent. or if one is consider "more beautiful" than the other.

Foi útil?

Solução

If you're certain that a variable's value is a single word (no white space) then it's OK to use $varname or ${varname}. If you can't guarantee this, then you should use "$varname" or "${varname}". Note that bash does word-splitting before interpreting your command, so you may actually get a syntax error if you don't quote the expression, for example

C="white space"
if [ -z $C ]
then
  ...
fi

will result in syntax error:

-bash: [: white: binary operator expected

while this works fine:

C="white space"
if [ -z "$C" ]
then
  ...
fi

This is due to the fact after variable expansion in the first, unquoted case bash sees this:

if [ -z white space ]
then
  ...
fi

and the -z operator expects just one, not two arguments. In the second, quoted case bash sees this:

if [ -z "white space" ]
then
  ...
fi

i.e. just a single argument as required. Note also that quotes were used in assignment

C="white space"

as it would also produce an error if you wrote

C=white space

since this would mean: execute command space with environment containing an added variable C=white.

So, in general you should quote these expressions to ensure your code is more robust against unforeseen variable values. This is especially true if the variable value comes from input, file etc. It is usually safe to drop the quotes for integer variables or when you just want to display the value of a variable as in echo $C.

Outras dicas

It matters when the string contains whitespace characters. Without the quotes, whitespace characters are treated as token delimiters and bash tries to interpret the substituted string as an expression.

Always put quotes to be safe, when you don't intend to evaluate the variable as a part of the expression.

Imagine you change the input from "example" to "two words", then you could encounter strange behaviour or even syntax errors when executing the script, in case you have overlooked the above.

In other words,

C="abc def"
# the echo command receives one argument: "abc def"
echo "$C"
# echo receives two arguments: "abc" and "def"
echo $C
# bash tries to execute the program "abc" with a first argument "def"
$C
# bash tries to execute the program "abc def"
"$C"

A good documentation about quotes and word-spliting :

"USE MORE QUOTES!" They are vital. Also, learn the difference between ' and " and `. See http://mywiki.wooledge.org/Quotes and http://wiki.bash-hackers.org/syntax/words

greybot sample from IRC freenode #bash is talking to the world =)

If it's a one-word constant, it's irrelevant.

However, you should read about the two kinds of quoting. Try this article and this documentation. There is also a SO question.

Try with a real example with whitespace. For the string example you do not need any quoting at all. So create a file called This is an example.txt and then retry. Substitute echo with ls...

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top