Getting the current Date in Your Scripts



I LIKE stamping things with the date, whether it’s a status report email or filename. (Like say I’ve downloaded todays news in Spanish, it really seems to make sense to tag it as spanish_news_12_8_08.mp3 instead of just spanish_news.mp3 doesn’t it?)

Okay, so this is something I do in a lot of my shell scripts. Here’s some code to play with.

Thankfully, linux has a great (and flexible) date command…

In the top part of your script just assign the variable date as such:

date=`date +%b%d%Y`

this uses the date command with the following format: Dec082008

Of course, there are other ways to format it – like date +%F which gives you 2008-12-08

As you’re seeing the +and then %letter formats the date. There are MANY different ways to format the date, in fact, if you just want the month, or day of week, that’s possible too. you might want to consult the manual page for date to see all the choices.

So… from the console type man date

now, you can have the date or time stamp you want in any of your shell scripts.

   Send article as PDF   

Similar Posts