Dates in Bash Script

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
nithinkk
Forum Commoner
Posts: 55
Joined: Sat Nov 28, 2009 7:57 am

Dates in Bash Script

Post 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 ?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Dates in Bash Script

Post by VladSun »

Code: Select all

date=`date +%_-d`
There are 10 types of people in this world, those who understand binary and those who don't
nithinkk
Forum Commoner
Posts: 55
Joined: Sat Nov 28, 2009 7:57 am

Re: Dates in Bash Script

Post by nithinkk »

Thanks :-)
qeemat
Forum Newbie
Posts: 10
Joined: Sat Feb 18, 2012 1:12 am

Re: Dates in Bash Script

Post 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
Post Reply