How to subtract a year from a date stored in a variable in shell script?

巧了我就是萌 提交于 2019-12-01 07:51:49

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/51259249/how-to-subtract-a-year-from-a-date-stored-in-a-variable-in-shell-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!