bash how to get yesterday's date


Date command has d parameter, that displays time described after d parameter:

date -d yesterday '+%Y-%m-%d'

%Y means year, %m means month, and %d means day. You can change formatting if you want:

date -d yesterday '+%Y:%m:%d'

What if you want get several days, for example day before 5 days?

date +%Y-%m-%d -d "5 day ago"

Would you take yesterday date to variable?

YESTERDAY=`date -d yesterday '+%Y-%m-%d'`

Alternative way is using hours:

date -d "24 hours ago" '+%Y-%m-%d'

Another way:

date --date='-1 day' '+%Y-%m-%d'