1. Introduction

Knowing the current weather conditions can be necessary for various reasons, such as planning outdoor activities, determining the best outfit for the day, or even deciding if it’s safe to travel.

While many websites and apps provide weather information, accessing the weather from the command line is a fast and efficient way to quickly check the current and future weather conditions.

In this tutorial, we’ll learn how to check the weather from the command line using multiple techniques.

2. Using curl and jq

To check the weather from the command line, we can use two commands in a pipeline, curl and jq.

curl is a command-line tool used to transfer data from or to a server while jq is a command-line JSON processor.

The weather information can be obtained from various APIs such as OpenWeatherMap, Weather Underground, Global Weather, and more.

Here, we’ll focus on two services with their interfaces:

  • OpenWeatherMap API
  • Global Weather API

Let’s start with the former.

2.1. Using the OpenWeatherMap API

To get data from OpenWeatherMap, we make a request to its URL including the necessary query parameters:

http://api.openweathermap.org/data/2.5/weather?q=<city_name>,<country_code>&appid=<our_api_key>

The q parameter represents the city name and country code separated by a comma, while the appid parameter holds the API key. We can sign up for a free API key on the OpenWeatherMap website.

Next, let’s see the actual curl call and its output with the -s silent switch:

$ curl -s "http://api.openweathermap.org/data/2.5/weather?q=San Francisco,US&appid=<our_api_key>"
{
    "coord": {
        "lon": -122.42,
        "lat": 37.77
    },
    "weather": [
        {
            "id": 802,
            "main": "Clouds",
            "description": "scattered clouds",
            "icon": "03d"
        }
    ],
    ...
}

Finally, we add jq via a pipe:

$ curl -s "http://api.openweathermap.org/data/2.5/weather?q=San Francisco,US&appid=<our_api_key>" | jq '.weather[0].description'
"scattered clouds"

The jq command formats the output and only displays the weather description.

2.2. Using the Global Weather API

Alternatively, we can also use curl with the Global Weather API:

$ curl -s 'https://www.meteosource.com/api/v1/free/point?place_id=london&sections=current%2Chourly&language=en&units=auto&key=<our_api_key>' -H 'accept: application/json' | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  5508  100  5508    0     0   8422      0 --:--:-- --:--:-- --:--:--  8422
{
  "lat": "51.50853N",
  "lon": "0.12574W",
  "elevation": 25,
  "timezone": "Europe/London",
...

Here, we’re using the place_id parameter to specify London as the location of interest. Then, we set the language using the en parameter. The last addition is the API key in the key parameter.

Finally, like before, we format the output using the jq command.

3. Using wttr.in

Another way to get weather information from the command line is by using wttr.in. It’s a weather forecast service that supports various information representation methods.

They include terminal-oriented ANSI sequences for console HTTP clients like curl or httpie. wttr.in also supports HTML for browsers and PNG for graphical viewers.

We can access the services from a shell or from a Web browser:

$ curl wttr.in/London
Weather report: London

      \   /     Sunny
       .-.      31 °C          
    ― (   ) ―   ↘ 6 km/h       
       `-’      10 km          
      /   \     0.0 mm         
...

We’re using curl to fetch weather information for London. We can specify a different country or city and we get a complete weather report, including forecasts for the next three days.

Omitting the location name generates weather information about our current location based on the IP address.

4. Using METAR

METAR is a format for reporting meteorological weather information in aviation.

Linux has the metar utility that receives encoded weather information from the Internet for one or more given airports and displays it in a human-readable format.

It isn’t available by default on most Linux distros, but we can install it from the local package manager.

Let’s install metar on Debian-based systems:

$ sudo apt install metar

Alternatively, on Arch Linux, we can use pacman:

$ sudo pacman -S metar

Now, let’s check the weather using metar:

$ metar -d HKJK
HKJK 081300Z 15012KT 9999 BKN026 22/14 Q1020 NOSIG
Station       : HKJK
Day           : 8
Time          : 13:00 UTC
Wind direction: 150 (SSE)
Wind speed    : 12 KT
Wind gust     : 12 KT
Visibility    : 9999 M
Temperature   : 22 C
Dewpoint      : 14 C
Pressure      : 1020 hPa
Clouds        : BKN at 2600 ft
Phenomena     : 

We’re using metar to get weather information from Jomo Kenyatta International Airport in Nairobi, Kenya.

Of course, we can get a full list of airport codes via the official IATA website and we can use that to query weather information from any airport around the world.

5. Using AnsiWeather

AnsiWeather is another Linux command-line utility we can use to display weather conditions.

It supports ANSI colors and Unicode symbols making it more visually appealing. AnsiWeather fetches its weather data from the OpenWeatherMap free API, which we used above.

Let’s first start by installing ANSI weather for Debian-based systems:

$ sudo apt-get install ansiweather

On the other hand, we can also install it on Arch Linux:

$ sudo pacman -S ansiweather

Now, let’s see weather conditions using the ansiweather command:

$ ansiweather -l London,GB -f 3
 London forecast => Fri Sep 08: 30/21 °C - Sat Sep 09: 32/22 °C - Sun Sep 10: 31/21 °C

We successfully fetched weather forecasts for London for the next three days.

6. Conclusion

In this article, we learned how to check the weather from the command line using the curl and jq commands to call different APIs offering weather forecast services. Further, the wttr.in forecast service provides a detailed weather report, including temperature, wind speed, humidity, and more.

In addition, we explored the METAR and AnsiWeather utilities. In particular, we installed and used them to fetch current and future weather conditions.

So, we can now quickly and easily retrieve current weather information for any city. Thus, we save time and effort.

Comments are closed on this article!