In a unix shell, how to get yesterday's date into a variable? In a unix shell, how to get yesterday's date into a variable? shell shell

In a unix shell, how to get yesterday's date into a variable?


dt=$(date --date yesterday "+%a %d/%m/%Y")echo $dt


On Linux, you can use

date -d "-1 days" +"%a %d/%m/%Y"


You can use GNU date command as shown below

Getting Date In the Past

To get yesterday and earlier day in the past use string day ago:

date --date='yesterday'

date --date='1 day ago'

date --date='10 day ago'

date --date='10 week ago'

date --date='10 month ago'

date --date='10 year ago'

Getting Date In the Future

To get tomorrow and day after tomorrow (tomorrow+N) use day word to get date in the future as follows:

date --date='tomorrow'

date --date='1 day'

date --date='10 day'

date --date='10 week'

date --date='10 month'

date --date='10 year'