← All Exercises

Loop Over a Date Range in Bash

intermediatebash~20min

Background

Report backfills and log rotation often need one iteration per day. Bash can drive that loop once dates are validated and advanced with a reliable tool.

Problem

Given a start date and an end date in YYYY-MM-DD form, print every date in the inclusive range together with its English weekday name.

Input

Two arguments in strict YYYY-MM-DD form with start not after end; the environment provides GNU date and runs in the C locale.

Output

One line per day as YYYY-MM-DD Weekday, inclusive of both endpoints; malformed, impossible, or reversed ranges exit non-zero.

Sample tests

Sample 1

Input: 2026-10-01 2026-10-03
Expected: 2026-10-01 Thursday
2026-10-02 Friday
2026-10-03 Saturday

Sample 2

Input: 2026-10-03 2026-10-01
Expected: start date is after end date
and exit status 1

Your solution

Learning objective

Validate dates beyond their shape, compare ISO strings safely, and advance a loop with GNU date arithmetic.