6

When I parameterize the date in the code as :

str_last_log_date="2017-07-24"
last_log_date=$(date -d '${str_last_log_date}' +%s)
threshold_days_ago=$(date -d 'now - 2 days' +%s)
echo "last_log_date ${last_log_date}  thres_days_ago ${threshold_days_ago}"

Gives the error :

date: invalid date ‘${str_last_log_date}’ last_log_date thres_days_ago 1500969455

But if I don't parameterize the date and pass directly, it gives the correct result :

last_log_date=$(date -d '2017-07-24' +%s)
threshold_days_ago=$(date -d 'now - 2 days' +%s)
echo "last_log_date ${last_log_date}  thres_days_ago ${threshold_days_ago}"

last_log_date 1500854400 thres_days_ago 1500969511

Any tips?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

2 Answers2

8

Variables are not expanded when put inside single quotes; use double quotes instead:

date -d "${str_last_log_date}"
heemayl
  • 56,300
4
last_log_date=$(date -d '${str_last_log_date}' +%s)

Should be updated to be (remove single quotes):

last_log_date=$(date -d ${str_last_log_date} +%s)
Yaron
  • 4,289