How do I check which years are leap years?

How can I find out which are leap years between 2014 and 2020 in a Linux terminal?

Is there any way using some code like $cal - anything to show which years are leap years between 2014 and 2020 straight away?


You can make use of date's exit code to check for a leap year, relying on date's behaviour of generating a non 0 exit code for an invalid date, obviosuly there's no 29th of Feb in a non-leap year:

date -d $year-02-29 &>/dev/null
echo $?

as a function:

isleap() { date -d $1-02-29 &>/dev/null && echo is leap || echo is not leap; }

Usage:

$ isleap 2019
is not leap

$ isleap 2020
is leap

Regarding your question:

How can I find out which are leap years between 2014 and 2020 in a Linux terminal?

echo "Leap years between 2014 and 2020:";
for y in {2014..2020}; do
    date -d $y-02-29 &>/dev/null && echo $y;
done

Just a variant of @RoVo's answer ...

for a in {2014..2020}
do 
  date -d $a-02-29 +"%Y" 2>/dev/null
done

date -d $a-02-29 +"%Y" 2> /dev/null sets date to the 29th of Feb and prints the year, ignoring any errors that occur.