![从今天的日期减去 7 天](https://linux22.com/image/110456/%E4%BB%8E%E4%BB%8A%E5%A4%A9%E7%9A%84%E6%97%A5%E6%9C%9F%E5%87%8F%E5%8E%BB%207%20%E5%A4%A9.png)
这是我的脚本。我需要从今天的日期中减去 7 天并将其用于文件名。我使用的是 Mac。
#/bin/bash
DATE=$(date -d "-7 days")
echo $DATE
当我运行这个 .sh 脚本时,我得到:
$ /Users/xxxxxxx/xxxxxxxx/dateTest.sh
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
答案1
你BSD date
需要不同的语法:
DATE="$(date -v-7d)"
在我的 FreeBSD 上man date
包括:
-v Adjust (i.e., take the current date and display the result of the
adjustment; not actually set the date) the second, minute, hour,
month day, week day, month or year according to val. If val is
preceded with a plus or minus sign, the date is adjusted forwards
or backwards according to the remaining string, otherwise the
relevant part of the date is set.