Ubuntu: Get Yesterday’s Date for a Bash Script CodeUnit 10 JAN 2011

Date work often plays an important part in automated bash scripts, and as such, it is pretty useful to be able to calculate yesterday’s date for your variable work.

Now we know in order to get the current date we could use this:

NOW="$(date +"%Y/%m/%d")"

Using this info, we find that it is remarkably simple to get yesterday’s date on any Linux system with:

YESTERDAY="$(date -d 'yesterday' +%Y/%m/%d)"

There are a range of other date manipulating strings available, including things like “3 days ago”, “next Monday”, “2 months”, etc, most of which can be found on the info date entry.


Related Posts:

About Craig Lotter

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.