2011-11-10 78 views
4

首先来看看这个问题: Bash or GoogleCL: new line in a string parameter击:变量在单引号

我想添加一个变量$ {日期}进入“摘要”现在:

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编写的命令行程序。我使用Python 2.6在Ubuntu 10.10上。

回答

4

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

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

@ This solution is BRILLIANT! – DocWiki

14

与其试图在单引号字符串内部扩展变量,典型的解决方案是连接单引号和双引号字符串。换句话说:

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

变量未在单引号内展开。要么你可以像威廉建议的那样做,或者你可以将行重写为双引号,这将扩展变量,只要你想。

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

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

现在我阅读了链接,并且您说\ n不会展开。一种认为解决办法是这样的:

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

这是用于此子shell有点粗糙,但它会节省你从你的backslashing报价。