Get the date (a day before current time) in Bash

Viewed 329747

How can I print the date which is a day before current time in Bash?

17 Answers
date -d "yesterday" '+%Y-%m-%d'

or

date=$(date -d "yesterday" '+%Y-%m-%d')
echo $date

You could do a simple calculation, pimped with an regex, if the chosen date format is 'YYYYMM':

echo $(($(date +"%Y%m") - 1)) | sed -e 's/99$/12/'

In January of 2020 it will return 201912 ;-) But, it's only a workaround, when date does not have calculation options and other dateinterpreter options (e.g. using perl) not available ;-)

Related