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:
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:
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
cd
to your home directory:cd ~/
- Open the file .bashrc in your preferred text editor (which is nano for me):
sudo nano .bashrc
- Add a new line with an alias:
alias weather='curl wttr.in'
- Save the change (Control-O in nano), then exit (Control+X)
- Run
source .bashrc
to add the function to the current terminal session2 - 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:
- In your home folder, run
unalias weather
to remove the alias we just set - Open .bashrc in your text editor
- Delete the line with
alias weather='curl wttr.in'
- Add a
weather()
function (you don’t need thealias
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
}
- Save and exit
- Run
source .bashrc
- 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
).3weather
with no argument returns a forecast based on your location.
I’m running macOS Big Sur, but this process, or something similar, should work on Linux too. ↩︎
More details on
source .bashrc
here. If it’s not there already, you’ll need to add something along the lines ofif [ -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. ↩︎weather "glasgow scotland"
andweather "new york"
will also work. ↩︎