简体   繁体   中英

Bash: Variable Expansion in Single Quote / Double Quote

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, , which makes it even more complicated. 后面 ,这使得它更加复杂。

Thanks a lot. I am on CentOS 6.

${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

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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