Bash: variable in single quote

First take a look at this question: Bash or GoogleCL: new line in string parameter

Now I want to add the variable $ {date} to "summary":

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'\' fine, though, don'\''t worry.' 

but the variable will not expand inside the single quote in bash.

Can this be done?

Note: GoogleCL is a command line program written in python. I'm on Ubuntu 10.10 with Python 2.6.

+4
python command-line linux bash googlecl
source share
3 answers

I will add another option to the list: define the variable as a new line, then use it inside double quotes.

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

Instead of trying to expand a variable inside a single quote, a typical solution is to combine single and double cylinder strings. In other words:

  'Today is' "$ {date}"'.  Poor '...
+14
source share

Variables do not expand in single quotes. Either you can do as William suggests, or rewrite the string in double quotes, which will expand the variable the way you want.

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

Bonus: this way you wonโ€™t have to avoid single quotes.

Now I read the link and you say that \ n will not expand. A workaround for this would be something like this:

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

Itโ€™s a little rude to use a subshell for this, but it will save you from flipping your quotes back.

+1
source share

All Articles