Bash:单引号/双引号中的变量扩展

发布于 2024-12-22 22:21:48 字数 267 浏览 1 评论 0原文

我想在下面的bash脚本中添加一个变量${date}

ffmpeg -i in.flv -vf drawtext="fontfile=Sans.ttf:text='Today is ${date}':fontsize=6" out.flv

显然,${date}不会在单引号中扩展,另请注意单引号之外还有双引号,这使得它变得更加复杂。

多谢。我在 CentOS 6 上。

I want to add a variable ${date} in the following bash script:

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.

Thanks a lot. I am on CentOS 6.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撧情箌佬 2024-12-29 22:21:48

${date} 被扩展,因为它位于双引号之间(双引号内的单引号只是字符)

测试它:

$ 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

${date} is expanded because it is between double quotes (the single quotes inside the double quotes are just characters)

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
入怼 2024-12-29 22:21:48

您的 ${date} 将正确扩展。正如您自己所说,您用双引号将整个字符串括起来,并且 bash 会将变量扩展为双引号。

事实上,有内部单引号根本不重要:

fg@erwin ~ $ ritchie="Goodbye world"
fg@erwin ~ $ echo "When Dennis passed away, he said '$ritchie'"
When Dennis passed away, he said 'Goodbye world'

Your ${date} WILL expand correctly. As you said yourself, you surround the whole string with double quotes, and bash will expand variables into double quotes.

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'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文