繁体   English   中英

Shell 脚本变量带有“”、()、{} 或两者一起用于带空格的值

[英]Shell Script variable with “”, (), {} or both together for value with space

shell 脚本中的变量使用“”或{}有什么区别

PATH="\home\user\test dir"

何时使用

"$PATH"
$(PATH)
${PATH}
"${PATH}" 

任何其他用例或方法来定义这个变量?

这是一个示例代码,其中{ }是必需的。

var=123
echo "${var}xx" # 123xx
echo "$var"xx   # 123xx but not the proper way
echo "$varxx"   # nothing, unknown variable varxx

并且$( )是一个命令替换: "$(cmd "foo bar")"导致命令 'cmd' 使用参数 'foo bar' 执行,并且"$(..)"将被 output 替换。 请参阅http://mywiki.wooledge.org/BashFAQ/002http://mywiki.wooledge.org/CommandSubstitution

总是引用你的变量。

了解如何在 shell 中正确报价,这非常重要:

“双引号”每个包含空格/元字符和每个扩展的文字: "$var""$(command "$var")""${array[@]}""a & b" 对代码或文字$'s: 'Costs $5 US'使用'single quotes' : 'Costs $5 US' , ssh host 'echo "$HOSTNAME"'
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM