[lug] Creating date variables in bash

Andrew F. Gilmore agilmore at wirelessbeehive.com
Tue Oct 12 10:56:23 MDT 2004


On Tue, 2004-10-12 at 10:47, Bill Thoen wrote:
> I can get today's date into a bash variable like so:
> 
> D=$(date +%G%m%d)
> 
> but how do I get the date for say, 7 days ago?
> 
> I tried 'man date' but didn't see an option for doing date math.

The GNU version of date, but not most others available out there, will
let you specify which date to display by specifying an argument like
--date='7 days ago' or -d '7 days ago'.

Very handy, and has simplified many a script I have written.

Only issue is that if you try to port that script to Solaris, for
example, you either have to install the GNU utils, or do something more
elaborate with your script.

It is in the man page, under option -d. In my page, it is the very first
option described.

Andrew





More information about the LUG mailing list