>_ IzzyLab

Hands-on task

This task is step 8 of the course “Dates and times in the terminal”

Last day of the month

medium~8 min

Write a script /usr/local/bin/eom that prints the last day of the month in YYYY-MM-DD format. With no arguments — for the current month, and if a date YYYY-MM-DD is passed — for that date's month (for example, eom 2025-03-08 prints 2025-03-31). Don't hardcode: the script must work for any date.

What you'll practise:
  • date
  • -d
  • last day of the month
  • relative shift
  • %F

Topic breakdown

date -d can add relative shifts to an absolute date: -d "2025-03-01 +1 month -1 day". The "last day of the month" idiom is built on this: the first day of the month (%Y-%m-01) is shifted one month forward and one day back — date arithmetic itself accounts for the different month lengths and for February in leap years, so there's no need to hardcode 28/30/31. A script that takes the date as an argument and defaults to today (${1:-…}) is easy to test on any month, not just the current one.

What's checked

  1. eom is executable
  2. no argument — the current month
  3. February, regular and leap year
  4. the 31st, the end of the month and December

What it looks like