Shell Script: Calculate Tomorrow’s Date on Unix

A couple of readers, upon reading this article to calculate yesterday’s date in a shell script, have requested for a similar script that can be used to calculate tomorrow’s date.

Even though such a script is quite straight forward to write, many people seem to be have problem with the leap year calculations. Anyway, I have managed to come up with a script to help everyone. Read on.

If you are running Linux, it’s highly likely that you have a GNU version of the date command installed. With GNU date, you can get tomorrow’s date quite easily (without the help of any shell script) by running it as follows:

$ date  -d "+1 day"
Thu Oct  8 06:26:25 PDT 2009

If you are running a commercial UNIX distribution instead of Linux, chances are that the date command will not support the ‘-d’ option. The script below can be used on such platforms to calculate tomorrow’s date instead. Save the following script to a file called tomorrow, chmod to 755 and copy it to a directory in your PATH.

Below are some examples on how the script can be called.

$ date
Wed Oct  7 06:36:45 PDT 2009

$ tomorrow
20091008

$ tomorrow "prefix %Y-%m-%d postfix"
prefix 2009-10-08 postfix

$ tomorrow "%Y-%m-%d"
2009-10-08

Update (09-Nov-2010): Script modified to run on Solaris platforms

Update (07-Aug-2011): Script updated to prevent printf function from interpreting certain numbers as octal

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, perl, php, python, java, javascript, nodejs, angular, react); plays_on(xbox, ps4); linux_desktop_user(true); }