问题描述
什么是关于脚本使用bash以下变量之间的区别:
$ VAR
$ VAR
$ {VAR}
$ {VAR}
之间存在 $ VAR
和 $ {VAR} 无差异code>和之间存在
$ VAR
和$ {VAR}
,除了没有区别在某些情况下,解析器可能无法识别您的意图,当你的用户以前的版本。试想一下:
富=您好
回声$ fooworld
回声$ {} foo的世界
第一个回声
打印什么,因为变量 fooworld
没有定义。第二打印的HelloWorld
因为shell是能够确定你引用富
变量。
之间的差异 $ VAR
和$ VAR
是不带引号的变量扩展被shell后评估的扩展。因此:
VAR ='LS /
$ VAR
列出 /
,由于扩建后壳评估空间作为标记分隔符,而
VAR ='LS /
$ VAR
在 LS /结果:没有这样的文件或目录
因为没有名字的命令 LS /
是用户的可用环境。
What are the differences between the following variables in regards to scripting with BASH:
$var
"$var"
${var}
"${var}"
There is no difference between $var
and ${var}
and there is no difference between "$var"
and "${var}"
, except that in certain cases the parser may not be able to identify your intent when you user the former versions. Consider:
foo=hello
echo "$fooworld"
echo "${foo}world"
The first echo
prints nothing, because the variable fooworld
is not defined. The second prints helloworld
because the shell was able to determine that you were referencing the foo
variable.
The difference between $var
and "$var"
is that unquoted variable expansions are evaluated by the shell after the expansion. As such:
var='ls /'
$var
Lists /
, because after the expansion the shell evaluates the space as a token separator, whereas
var='ls /'
"$var"
Results in ls /: No such file or directory
because no command named ls /
is available in the user's environment.
这篇关于Bash的变量格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!