Getting the Weather And Forecast with A Bash Script

With various services shutting down their public/free APIs, my various weather scripts stopped working. Which, y’know, Linux, so I could find something to replace it easily, right?

Yeah, well, not so much.

There are some pretty nice programs out there, but everything I came across didn’t quite fit my needs – either it was only an applet, or was super resource heavy, or had to be specifically launched, or the output wasn’t quite how I wanted it.

So, y’know. Linux.

I created these scripts to get the current conditions and the forecast using the OpenWeatherMap API (free signup) and is written in bash (with some really common dependencies that are probably in your package manager).

weather.sh gets the current weather from OpenWeatherMap and displays the results to the terminal, HTML, or for an OpenBox pipe menu. It will calculate (if appropriate) the “feel like” weather by calculating the wind chill or heat index. A great deal of basis for this script comes from BashWeather, bash-weather, and many more that I forgot to save the URLs of.

forecast.sh gets the forecast from OpenWeatherMap and likewise displays the results. OpenWeatherMap forecasts are at three hour intervals (0800, 1100, 1400, etc). It displays all forecasts for the next twenty four hours, then displays the closest time forecast for the day after that. For example, if it’s 1136 (as I’m writing this) it shows all forecasts through the 1100 forecast the next day, then the 1100 forecast for each subsequent day. Options and .rc file are the same as for weather.sh.

You can also have colorized terminal output, as demonstrated below.

The colorization – derived from this gist – is easily changed and tweaked. Take a look here:

echo "As Of: ${YELLOW}$AsOf ${RESTORE}"  
echo "Right Now: ${CYAN}$icon $LongWeather${RESTORE}"

All you’d have to do to reverse them is to replace ${CYAN} with ${YELLOW} and vice versa.

Which is the other point of why I wrote these – I wanted something that, while feature-rich, was pretty easy to understand and hack on for the next person who came along. The colorization is also designed so that you can easily source it in your own scripts and use it as a library.

You can get the programs at GitHub, GitLab, or my own git repository.