2
as_of_dt='2016-01-01'
as_of_dt_prev_year=$($as_of_dt -d '-1 year' +'%Y-%m-%d')
echo $as_of_dt_prev_year

This does not work. error: -d: command not found

However this works if we use '$date' instead of $as_of_dt.

NaN
  • 7,441
  • 6
  • 32
  • 51
Don Sam
  • 525
  • 5
  • 20

1 Answers1

2

Played around with it. This seems to work:

as_of_dt='2016-01-01'
as_of_dt_prev_year=$(date --date="${as_of_dt} -1 year" +'%Y-%m-%d')
echo $as_of_dt_prev_year

Note the double quotes that are needed for variable substitution to work.

NaN
  • 7,441
  • 6
  • 32
  • 51
  • It is still not clear to me. How will you assign it to this variable - as_of_dt_prev_year – Don Sam Jul 10 '18 at 07:40
  • This question should clarify how to assign the result of the date command to a variable : https://stackoverflow.com/questions/20688552/assigning-the-output-of-a-command-to-a-variable#20688600. Indeed you missed the date command in your second line. – NaN Jul 10 '18 at 07:58