Page 1 of 1

Dates in Bash Script

Posted: Tue Mar 08, 2011 12:41 am
by nithinkk
I'm finding difficulty in getting the dates in my bash script .

Code: Select all

 date=`date +"%d"`  

The above code will output 08....but i want only 8 not zero !!! how to do it ?


Till yesterday i was getting it by doing

Code: Select all

  date1=$[date*1]  
But now i'm not getting a value from it.....Can any one help me ?

Re: Dates in Bash Script

Posted: Tue Mar 08, 2011 4:17 am
by VladSun

Code: Select all

date=`date +%_-d`

Re: Dates in Bash Script

Posted: Tue Mar 08, 2011 6:13 am
by nithinkk
Thanks :-)

Re: Dates in Bash Script

Posted: Mon Feb 20, 2012 6:34 am
by qeemat
The new short way:
$ date -d '1 day ago' +'%Y/%m/%d'
2009/07/21

Or the longer way:
Yesterday in epoch seconds
$ yesterday=$((`date +'%s'` - 86400))

Get default formatted yesterday's date
$ date -d "1970-01-01 $yesterday sec"
Tue Feb 17 01:27:32 PST 2009

Same thing in YY-MM-DD
$ date -d "1970-01-01 $yesterday sec" +"%Y-%m-%d"
2009-02-17