Often you may want to compare two dates in Bash.
You can use the following methods to do so:
Method 1: Compare Two Dates
[[ "$date1" < "$date2" ]] && echo "date1 is earlier" || echo "date1 is later"
This particular example checks if the date stored in the variable named $date1 is less than (i.e. “earlier than”) the date stored in the variable named $date2 and outputs the appropriate result.
Method 2: Compare Date with Today
[[ "$my_date" < "$(date +%F)" ]] && echo "date is before today" || echo "date is after today"
This particular example checks if the date stored in the variable named $my_date is less than today’s date.
The following examples show how to use each method in practice.
Example 1: Compare Two Dates in Bash
Suppose that we have the following two dates in Bash:
- $date1: 2024-01-25
- $date2: 2024-04-13
We can use the following syntax to check if $date1 is less than (i.e. “earlier than”) $date2:
date1=2024-01-25
date2=2024-04-13
[[ "$date1" < "$date2" ]] && echo "date1 is earlier" || echo "date1 is later"
The following screenshot shows how to use this syntax in practice:

The statement returns “date1 is earlier” since the date stored in date1 (January 25, 2024) is indeed earlier than the date stored in date2 (April 13, 2024).
Example 2: Compare Date with Today in Bash
Suppose that we would like to compare the following two dates in Bash:
- $my_date: 2024-10-30
- Today’s date: 2024-04-15
We can use the following syntax to check if $my_date is less than (i.e. “earlier than”) today’s date:
my_date=2024-10-30
[[ "$my_date" < "$(date +%F)" ]] && echo "date is before today" || echo "date is after today"
The following screenshot shows how to use this syntax in practice:

The statement returns “date is after today” since the date stored in my_date (October 30, 2024) is indeed later than today’s date (April 15, 2024).
Note: We used $(date +%F) to get today’s date in Bash.
Related Tutorials
The following tutorials explain how to perform other common tasks in Bash:
Bash: How to Compare Two Files
Bash: How to Count Number of Unique Lines in File
Bash: How to Count Files in Directory Matching a Pattern
