如何从存储在shell脚本变量中的日期减去一年? [英] How to subtract a year from a date stored in a variable in shell script?
本文介绍了如何从存储在shell脚本变量中的日期减去一年?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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
这不起作用.错误:-d:找不到命令
This does not work. error: -d: command not found
但是,如果我们使用'$ date'而不是$ as_of_dt,这是可行的.
However this works if we use '$date' instead of $as_of_dt.
推荐答案
随它一起玩.这似乎可行:
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.
这篇关于如何从存储在shell脚本变量中的日期减去一年?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文