Monthly Archives: March 2018

Difference in “date” function implementation in Mac OSX and Ubuntu bash

A few weeks ago, I had written some bash code in Max OSX terminal to identify the current date, and then defining the date a few days back in time. When I tried to run the same code in Ubuntu bash terminal, the code line for identifying the previous date fails. Some brief time spent on Google told me that there is some difference in how the “date” function is implemented in Mac OSX and Ubuntu bash. The correct usage is as follows:

curr_date=$(date +”%Y%m%d”)
echo ‘Current date: ‘$curr_date

# Find the date 3 days ago – For OSX bash:
prev_date_OSX=$(date -v-3d +”%Y%m%d”)
echo ‘Previous date: ‘$prev_date

# Find the date 3 days ago – For Linux Ubuntu bash:
prev_date=$(date -d “3 days ago” +”%Y%m%d”)
echo ‘Previous date: ‘$prev_date

Advertisements