Check Weather Like a Pro: A Simple Bash Script for Your Terminal

ยท

3 min read

Have you ever wanted to check the weather forecast directly from your terminal? In this blog, I'll show you how to create a simple Bash script that fetches weather details for any city using the free wttr.in weather service.

Why Use a Bash Script for Weather?

Instead of opening a browser and searching for the weather, you can get instant results with a single command. This script is lightweight, fast, and efficiently automates weather checking.

Prerequisites

Before running the script, ensure you have:

  • A Unix-based system (Linux/macOS)

  • curl installed (most systems have it by default)

The Bash Script

Bash script to fetch weather details:

#!/bin/bash

# Function to display usage
show_usage()   
{
   echo "Usage: $0 <city_name>"
   echo "Example: $0 Delhi"
}

# Check if city is provided
CITY="$1"              # Gets city as arrgument.
if [ -z "$CITY" ];     # City should not be empty.
then
    echo "Error: City name not provided."
    show_usage         # Show the usage/guide the user.
    exit 1
fi

# Displaying the loading message
echo "Fetching weather details for $CITY..."

# Fetching weather data for a city
WEATHER=$(curl -s "https://wttr.in/${CITY}?format=3")  # fetch weather data from wttr.in in a simple text format.

# Checking if the response is valid
if [[ "$WEATHER" == *"Unknown location"* ]];   # If the city name is invalid, it displays an error message
then
    echo "Error: Unable to fetch weather details for '$CITY'. Please check the city name."
    exit 1
fi

# Display the weather details
echo "====================================="
echo "            WEATHER REPORT           "
echo "====================================="
echo "        $WEATHER"
echo "====================================="

# Suggest viewing the full report
echo
echo "NOTE: For more details, visit: https://wttr.in/${CITY}"
echo

How It Works

  1. The script takes a city name as an argument.

  2. It uses curl to fetch weather data from wttr.in in a simple text format.

  3. If the city name is invalid, it displays an error message.

  4. The script presents a formatted weather report directly in the terminal.

  5. It also provides a link to view the full weather report online.

Running the Script

Save the script as weather.sh, then provide execution permissions:

chmod +x weather.sh

Now, you can check the weather for any city:

./weather.sh London

Example Output

Fetching weather details for London...
=====================================
            WEATHER REPORT           
=====================================
        London: ๐ŸŒฆ +9ยฐC
=====================================

NOTE: For more details, visit: https://wttr.in/London

Enhancements & Customization Ideas

  • Add Color: Use tput or ANSI escape codes to enhance readability.

  • Auto-detect Location: Use curl -s ipinfo.io/city to fetch the user's current city.

  • Integrate Notifications: Display alerts using notify-send on Linux or osascript on macOS.

Conclusion

With just a few lines of Bash scripting, you can create a handy weather-checking tool. This script can be further customized to suit your needs. Try it out and let me know how you improve it!

Happy scripting! ๐Ÿš€

ย