1

I'm following this example to parameterize date. But this code:

FROM_DATE=$(date --date='1 day ago' +"%F")

OPTARG="1 day ago"
echo $OPTARG
FROM_DATE=$(date --date=${OPTARG} +"%F")
echo $FROM_DATE

gives me an error:

date: extra operand ‘ago'’

How should I fix it?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
hudi
  • 15,555
  • 47
  • 142
  • 246
  • 1
    Oh duh, I missed the quoting problem. This is a FAQ. See also https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable – tripleee Aug 30 '17 at 10:52

1 Answers1

3

Quote the expansion of $OPTARG. Without quotes, --date=1 day ago is split into three arguments: --date=1, day, and ago.

FROM_DATE=$(date --date="$OPTARG" +"%F")
John Kugelman
  • 349,597
  • 67
  • 533
  • 578