How to write a shell script where to add days month and year to current date How to write a shell script where to add days month and year to current date shell shell

How to write a shell script where to add days month and year to current date


Tip: fire up a terminal(in my case bash terminal)

the help is a good starting point

date --help

or man page

man date

A lot of information and examples.

Date manipulation in bash (copy paste the example run in your terminal):

add 10 days to the current date:

date -d "10 day" +"%Y %m %d"

or remove 10 days to the current date

date -d "-10 day" +"%Y %m %d"

add 2 months to the current date:

date -d "2 month" +"%Y %m %d"

remove 2 months from the current date:

date -d "-2 month" +"%Y %m %d"

add 1 year to the current date

date -d "1 year" +"%Y %m %d"

remove 1 year to the current date

date -d "-1 year" +"%Y %m %d"

mixing add 1 year month and day

date -d "1 year 1 month 1 day" +"%Y %m %d"

in a script (in my case bash)

foobaa=`date -d "1 year 1 month 1 day" +"%Y %m %d"`echo $foobaa

I hope it helps a little..


The equivalent on Mac OSX is date -v "+60M" to add 60 minutes.


Simple command in short is
date -d "+3 days +4 Month +2 Years"

Similarly, you can also subtract dates too.
date -d "-3 days -4 Month -2 Years"