Bash: Display a Calendar with cal and ncal

๐Ÿ“… June 12, 2024
What is today’s date? Let’s open a terminal…

Bash is cool. Linux is cool. There is seemingly a program for everything you might want to do in Linux. Of course, there is not, but it seems like it.

What if you need to view a calendar from within a terminal because you want to show off to your friends or apply it to a script?

Linux provides the cal and ncal programs that will show a calendar from the command line in a variety of ways depending upon the options you use.

cal and ncal: What is the Difference?

How the calendar is displayed and the options allowed.

cal
ncal
ncal -b

By default, cal and ncal show the current month. The difference is in the display. cal is the traditional layout, and ncal is an alternative vertical layout. The current day is highlighted in ncal by default. If you need the traditional layout plus highlighting and the ability to use ncal’s options, add the -b option to ncal.

Linux is all about freedom and choices, and these layouts are available for what you might need.

The Options

cal and ncal cannot use the same options. Some work, but not all. For example, suppose you want to get the date on which Easter falls for the current year. According to the man page, we use the -e option.

cal -e    # Does not work. -e not supported with cal
ncal -e   # This works

Output from ncal -e

03/31/2024

For the Orthodox Easter date, use the -o option.

cal -o    # Does not work with cal
ncal -o   # This works

Output from ncal -o

05/05/2024

This is an example of an option not being compatible with both cal and ncal even though the man page lists it for both. (The man page warns about this incompatibility.) Just experiment. If an option does not work, you will know about it.

The same man page applies to both programs.

man ncal
man cal

Julian Days

What day of the year is it? This is answered by the Julian calendar using the -j option.

ncal -j
cal -j

June 12, 2024 is Day 164, Wednesday, according to the Julian calendar.

Get a Full Year Calendar

If you need to display a calendar for the entire year, use the -y option followed by the year you wish to view.

cal -y 2020
ncal -y 2020

Each month from 2020 is displayed.

Combining Options

We can combine a few options to customize the result.

ncal -B1 -H 2024-06-12

An extra month and a highlighted date.

  • -B1 adds one month before the current month so we can see it.
  • -H yyyy-mm-ddย  Highlights the given date

With -H, we can specify any date to highlight. This does not have to be the current date. We can use command substitution with date to get the current date.

ncal -B1 -H $(date '+%Y-%m-%d')

$(date ‘+%Y-%m-%d’) produces the same result as a hardcoded date, but this makes a script more flexible so the current day is highlighted no matter when the script is executed.

ncal -B1 -A1 -H $(date '+%Y-%m-%d')
  • -A1 specifies how many months to add after the current month. When used with -B1, we can see the month before and the month after the current month.

Show the month before and after the current month in addition to highlighting the current date.

Colorizing the Output

Why not add a splash of color? Install lolcat and pipe ncal to it.

ncal -B1 -A1 -H $(date '+%Y-%m-%d') | lolcat -p 0.1 -F 0.01

Various lolcat options. Note that lolcat will remove the highlighted date.

Instead of using -B1 and -A1 we can use the shorthand -3 to achieve the same result.

cal -3

Showing previous, current, and following months using cal.

When is Christmas?

Yes, we know that it is December 25th, but on what day will it fall for the current year?

ncal -m 12 -3 -H $(date '+%Y-12-25') -b

Show the adjacent months for reference.

  • -m 12ย  Specifies the month to show.
  • -3 Show the preceding and following months.
  • -H $(date ‘+%Y-12-25’)ย  Which date to highlight? Get the current year while keeping the month and day (December 25) constant.
  • -bย  We need this for ncal in order to display the traditional layout. Otherwise, we would see the vertical layout.

Even though we are running ncal in June 2024, we can show any month we choose with the proper options.

Full Julian Year

This type of calendar is seen often in manufacturing industry where we need the day number of the year.

ncal -y 2024 -jb

June 12, 2024 is Day 164 in year 2024. Since 2024 is a leap year we see Day 366 is the last day of 2024 on Tuesday. Views like this are useful for those who need them.

Which Day is Christmas on the Julian Calendar?

The Julian calendar only shows day numbers within the year, so specific days, such as birthdays and Christmas, are not obvious. To highlight a given date, we use the -H option.

ncal -y 2024 -jb -H $(date '+%Y-12-25')-12-25

Christmas falls on day 360, Wednesday, in year 2024 on the Julian calendar.

We can highlight any day we like using -H. We can specify only one date to highlight.

Country Codes and Switch Dates

ncal -p

The -p option shows the dates on which the displayed countries switched from the Julian calendar to the Gregorian calendar. Differences exist between the calendars and this can cause discrepancies in some situations, so this provides a quick lookup for reference.

Conclusion

ncal is probably what you would want to use most of the time given its better option support. To get a traditional layout using ncal, add the -b option. With cal and ncal, we have multiple ways of displaying similar calendars in a terminal to show off some cool skills without using a GUI.

Have fun!

 

,

  1. Leave a comment

Leave a comment