Skip to content

A calendar and weather forecast in the terminal

A couple of cool terminal tricks to get calendars and the weather, plus how to assign aliases and pass arguments to bash functions.

I learned a couple of cool terminal tricks on Hacker News yesterday.

Calendar permalink

The first is the cal program, which prints an ASCII calendar.

cal by itself returns the current month with today’s date highlighted.

cal 2022 returns a calendar for the whole of 2022.

cal -3 returns the current and surrounding months. This may be the most useful default view for me:

Running 'cal -3' returns a calendar comprising the previous month, current month and next month.
Running cal -3 returns a calendar comprising the previous month, current month and next month.

It also works for the past: cal 1981 returns a calendar for 1981. You can also capture just a month: cal may 1981.

The weather permalink

Another user brought up wttr.in, a service which provides a weather report in ASCII format.

You can run a simple curl wttr.in to retrieve a forecast based on your location:

A three-day weather forecast for Edinburgh.
A three-day weather forecast for Edinburgh.

Assigning alias weather to curl wttr.in permalink

To make the curl wttr.in command more memorable, I decided to assign the alias weather to it. Adding aliases can be useful in general, so I thought I’d document the process:1

  1. cd to your home directory: cd ~/
  2. Open the file .bashrc in your preferred text editor (which is nano for me): sudo nano .bashrc
  3. Add a new line with an alias: alias weather='curl wttr.in'
  4. Save the change (Control-O in nano), then exit (Control+X)
  5. Run source .bashrc to add the function to the current terminal session2
  6. You can now run weather to get a weather forecast for your current location

Passing an argument for location permalink

wttr.in also lets you query the weather in a specific location. I updated my weather command to allow for this:

  1. In your home folder, run unalias weather to remove the alias we just set
  2. Open .bashrc in your text editor
  3. Delete the line with alias weather='curl wttr.in'
  4. Add a weather() function (you don’t need the alias keyword):
weather() {
if [ $# -eq 0 ] # If no argument has been passed to this function
then
curl wttr.in
else
curl wttr.in/"$1" # Append location
fi
}
  1. Save and exit
  2. Run source .bashrc
  3. Now, by adding a location argument to weather you can see what the weather is like in, say, Glasgow (weather glasgow+scotland) or New York (weather new+york).3 weather with no argument returns a forecast based on your location.

  1. I’m running macOS Big Sur, but this process, or something similar, should work on Linux too. ↩︎

  2. More details on source .bashrc here. If it’s not there already, you’ll need to add something along the lines of if [ -f .bashrc ]; then source .bashrc; fi to .zshrc or .bash_profile so that aliases are available to you in the terminal automatically whenever you log into the OS. In my case I also had to create the .zshrc file. ↩︎

  3. weather "glasgow scotland" and weather "new york" will also work. ↩︎