Do I need to quote command substitutions?

后端 未结 3 994
借酒劲吻你
借酒劲吻你 2021-01-05 22:44

According to the Google Shell Style Guide, I should:

Always quote strings containing variables, command substitutions, spaces or shell meta characters

3条回答
  •  忘掉有多难
    2021-01-05 23:25

    EDIT: (A command substitution is $( ) and the back-tick almost-equivalent, so you're likely interpreting it correctly)

    It will certainly not hurt to quote strings in general in shell scripts, unless you're actually relying on globbing etc. to take effect.

    In this simple example, the double-quotes are certainly not needed, but for consistency I would probably add them.

    If you instead have

    VAR=$( echo $foo $bar )
    

    ... then I would certainly quote both variables and expression:

    VAR="$( echo "$foo" "$bar" )"
    

    especially if any of those variable contained external input or if you knew they had globbing-characters in them.

    EDIT: As user @CharlesDuffy points out, the outer double-quotes here are still not needed. I would still add them to be consistent with other variable assignments that do need quoting.

提交回复
热议问题