Bash:单个qu中的变量

2024-06-07 10:21:57 发布

您现在位置:Python中文网/ 问答频道 /正文

首先看看这个问题: Bash or GoogleCL: new line in a string parameter

我想现在在“摘要”中添加一个变量${date}:

google youtube post ~/videos/cat-falls-down-stairs.avi Comedy \
    --tags 'currency of the internet' \
    --summary $'Today is ${date}. Poor whiskers takes a tumble.\nShe'\''s fine, though, don'\''t worry.'

但在bash中,变量不会在单引号内展开。

能做到吗?

注意:GoogleCL是用python编写的命令行程序。我在Ubuntu10.10和Python2.6上。


Tags: orinbashnewdatestringparameteryoutube
3条回答

典型的解决方案不是试图在单引号字符串中展开变量,而是将单引号和双引号字符串连接起来。换句话说:

'Today is'"${date}"'. Poor' ...

变量不在单引号内展开。您可以像William建议的那样做,也可以将行重写为双引号,这将根据需要展开变量。

"Today is ${date}. Poor whiskers takes a tumble.\nShe's fine, though, don't worry."

奖励:这样做你就不必逃避你的单引号。

现在我读了链接,您说不会扩展。解决方法如下:

--summary $(echo -e "Today is...")

使用subshell来实现这一点有点粗糙,但它可以避免反斜杠引用。

我将向列表中添加另一个选项:将变量定义为换行符,然后在双引号内使用它。

nl=$'\n'
...
   --summary "Today is ${date}. Poor whiskers takes a tumble.${nl}She's fine, though, don't worry."

相关问题 更多 >

    热门问题