Unix shell scripting: date format syntax

325 Views Asked by At

I m trying to get yesterday date, it's not working in hp ux server.

Prev_date=$(date +"y%m%d" -d "1 day ago")

For this I m still getting current date only. 20210811

Could you please help on the same.

2

There are 2 best solutions below

2
Dominique On

You missed a percentage in front of the 'y': this is working fine for me:

echo $(date +"%y%m%d" -d "1 day ago")
1
vkeeWorks On

You can use below command, if you want.

date --date=' 1 days ago' '+%Y-%m-%d'

It will give result like

 2021-09-06

I prefer this format since most of the time my scripts include SQL queries for data fetch and hence date is required to filter out data on daily basis.