Bash的变量格式 [英] Bash variable formats

查看:100
本文介绍了Bash的变量格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是关于脚本使用bash以下变量之间的区别:

  $ VAR

 $ VAR

  $ {VAR}

 $ {VAR}


解决方案

之间存在 $ VAR $ {VAR} 和之间存在$ 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的变量格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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