Linux Bash:单引号/双引号中的变量扩展
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8600958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash: Variable Expansion in Single Quote / Double Quote
提问by Susan Mayer
I want to add a variable ${date}
in the following bash script:
我想${date}
在以下 bash 脚本中添加一个变量:
ffmpeg -i in.flv -vf drawtext="fontfile=Sans.ttf:text='Today is ${date}':fontsize=6" out.flv
Obviously, ${date}
won't expand in single quote, please also note that there is a double quote beyond the single quote, which makes it even more complicated.
显然,${date}
不会在单引号中展开,还请注意单引号之外还有一个双引号,这使得它更加复杂。
Thanks a lot. I am on CentOS 6.
非常感谢。我在 CentOS 6 上。
采纳答案by Matteo
${date}
is expanded because it is between double quotes (the single quotes inside the double quotes are just characters)
${date}
展开是因为它在双引号之间(双引号内的单引号只是字符)
Test it with:
测试它:
$ export date=SOMEVALUE
$ echo ffmpeg -i in.flv -vf drawtext="fontfile=/usr/share/fonts/dejavu/DejaVuLGCSans.ttf:text='Today is ${date}':fontsize=6" out.flv
ffmpeg -i in.flv -vf drawtext=fontfile=/usr/share/fonts/dejavu/DejaVuLGCSans.ttf:text='Today is SOMEVALUE':fontsize=6 out.flv
回答by fge
Your ${date}
WILL expand correctly. As you said yourself, you surround the whole string with double quotes, and bash willexpand variables into double quotes.
你的${date}
遗嘱正确展开。正如你自己所说,你用双引号将整个字符串括起来,bash会将变量扩展为双引号。
The fact that there are inner single quotes does not matter at all:
有内部单引号的事实根本无关紧要:
fg@erwin ~ $ ritchie="Goodbye world"
fg@erwin ~ $ echo "When Dennis passed away, he said '$ritchie'"
When Dennis passed away, he said 'Goodbye world'