2
DATE="1 week ago"
date --date='$DATE'

doesn't work. How can I get it to work?

I could do:

DATE_CMD="date --date='$DATE'"
eval $DATE_CMD

but I don't want to store the entire command in a variable.

Ash
  • 2,021
  • 2
  • 26
  • 59

2 Answers2

2

You just need to use double-quotes to enable string interpolation:

date --date="$date"
nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • 1
    Another quick response. Yes, this works too. Thought I'd direct readers to this topic which explains it quite nicely: http://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash – Ash Sep 24 '13 at 07:07
2

You're a victim of quote expansion.

The proper invocation would likely be:

DATE='1 week ago'
date --date="$DATE"

(notice the double quotes)

JB.
  • 40,344
  • 12
  • 79
  • 106
  • 1
    Wow! That was the quickest solution I've ever received! Can't even accept your answer yet since I have to wait 11 minutes (apparently). – Ash Sep 24 '13 at 07:02