Quoting vs not quoting the variable on the RHS of a variable assignment

后端 未结 5 1678
孤街浪徒
孤街浪徒 2020-12-16 14:12

In shell scripting, what is the difference between these two when assigning one variable to another:

a=$b

and

a=\"$b\"
         


        
5条回答
  •  萌比男神i
    2020-12-16 14:55

    There are no (good) reasons to double quote the RHS of a variable assignment when used as a statement on its own.

    The RHS of an assignment statement is not subject to word splitting (or brace expansion), etc. so cannot need quotes to assign correctly. All other expansions (as far as I'm aware) do occur on the RHS but also occur in double quotes so the quoting serves no purpose.

    That being said there are reasons not to quote the RHS. Namely how to address error "bash: !d': event not found" in Bash command substitution (specifically see my answer and rici's answer).

提交回复
热议问题