在变量分配的RHS中引用与不引用变量 [英] Quoting vs not quoting the variable on the RHS of a variable assignment

查看:82
本文介绍了在变量分配的RHS中引用与不引用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在shell脚本中,将一个变量分配给另一个变量时,这两者之间有什么区别?

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

a=$b

a="$b"

什么时候应该在另一个上使用?

and when should I use one over the other?

推荐答案

我认为这里没有太大区别.是的,建议在引用变量时将其括在双引号中.但是,在您的问题中似乎未引用$x.

I think there is no big difference here. Yes, it is advisable to enclose a variable in double quotes when that variable is being referenced. However, $x does not seem to be referenced here in your question.

y=$x本身并不影响空白的处理方式.只有在实际使用$y时,引用才有意义.例如:

y=$x does not by itself affect how whitespaces will be handled. It is only when $y is actually used that quoting matters. For example:

$ x=" a    b "
$ y=$x
$ echo $y
a b
$ echo "$y"
 a    b

这篇关于在变量分配的RHS中引用与不引用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆