8 Dec 2015
GNU's date
tool offers a few additional features over the standard abilities of the traditional UNIX date
facility. One of these is the "+%j
" switch, which tells you the day's number as a day-of-the-year. So the first of January is "1", the second is "2", and so on. The first of February is "31", and Christmas is "359" - that is, it's the three-hundred-and-fifty-ninth day of the year (at least it is this year; it would be the 360th on a Leap Year).
The "-d dd-mmm
" option allows you to specify a date other than today; it is remarkably clever in what it can understand. In this example, we just give it "25-Dec" which it will always interpret as "Christmas, this year".
This simple script takes the difference between today and Christmas this year, and tells you the time until Christmas.
Note that in the last week of the year, it will give you a negative number; you may want to tweak this to give the days until next Christmas.
#!/bin/bash TODAY=`date +%j` # Today, as day-of-year (1-366) CHRISTMAS=`date -d 25-Dec +%j` # Christmas day, in same format echo "There are $(($CHRISTMAS - $TODAY)) days until Christmas."
My Shell Scripting books, available in Paperback and eBook formats. This tutorial is more of a general introduction to Shell Scripting, the longer Shell Scripting: Expert Recipes for Linux, Bash and more book covers every aspect of Bash in detail.
![]() Shell Scripting Tutorial is this tutorial, in 88-page Paperback and eBook formats. Convenient to read on the go, and in paperback format good to keep by your desk as an ever-present companion. Also available in PDF form from Gumroad:Get this tutorial as a PDF | ![]() Shell Scripting: Expert Recipes for Linux, Bash and more is my 564-page book on Shell Scripting. The first half covers all of the features of the shell in every detail; the second half has real-world shell scripts, organised by topic, along with detailed discussion of each script. |