$ echo $SHELL
/bin/sh
$ YESTERDAY=`date -v -1d "+%Y%m%d"`
$ echo $YESTERDAY
20170201
$ other_dayd="2d"
$ OTHERDAY=`date -v -$other_dayd "+%Y%m%d"`
$ echo $OTHERDAY
20170131
Is there a way to substitutedate -v -1d
withdate -v -$other_dayd
?
other_day=2
date -v -${other_day}d "+%Y%m%d"
$ other_day=2
$ OTHERDAY=`date -v -$other_dayd "+%Y%m%d"`
-: Cannot apply date adjustment
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]
In his example the variable other_dayd contains 2d, so the argument becomesIn your example though how does date(1) "figure out" that the value of $other_dayd should be interpreted as "d" for "day"?
-2d
when the variable is expanded, and is interpreted by the date command as normal.$ echo $SHELL
/bin/sh
$ YESTERDAY=`date -v -1d "+%Y%m%d"`
$ echo $YESTERDAY
20170209
$ other_day="2"
$ OTHERDAY=`date -v -${other_day}d "+%Y%m%d"`
$ echo $OTHERDAY
20170208
$ other_day="-3"
$ OTHERDAY=`date -v ${other_day}d "+%Y%m%d"`
$ echo $OTHERDAY
20170207