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
  • 1,867
  • 2
  • 21
  • 49

2 Answers2

2

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

date --date="$date"
nneonneo
  • 162,933
  • 34
  • 285
  • 360
  • 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.
  • 37,635
  • 10
  • 80
  • 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