Have you ever wanted to quickly check the current temperature outside without having to open a web browser or a dedicated weather app? If so, then you're in luck! In this article, we'll show you how to use the command-line tool curl to display the current temperature value using the popular weather service wttr.in.
Before we dive into the details, let's first make sure that you have the necessary tools installed on your system. curl is a command-line tool that allows you to transfer data between a server and your local machine. It is pre-installed on most modern Linux distributions, as well as macOS. If you are using Windows, you can install curl by following the instructions on the official website.
What is wttr.in?
wttr.in is a simple and easy-to-use weather service that provides current weather information for a wide range of locations around the world. It is accessible via a web browser or through the command line using curl. One of the great things about wttr.in is that it provides a wealth of information beyond just the current temperature, including the wind speed and direction, humidity, and precipitation. However, in this article, we'll focus on how to display just the current temperature value.
Using curl to Display the Current Temperature
To display the current temperature using curl and wttr.in, you can use the following command:
curl wttr.in/[LOCATION]?0
Replace [LOCATION] with the name of the location you want to check the weather for. For example, if you want to check the current temperature in New York City, you can use the following command:
curl wttr.in/New\ York?0
The ?0 at the end of the command tells wttr.in to only display the current temperature value. Without this parameter, wttr.in will display a full weather report, which can be useful if you want more detailed information. However, for a quick temperature check, the ?0 parameter is all you need.
Formatting the Output
By default, the output of the curl command is displayed in the terminal window. However, you can format the output to make it easier to read or to use in other applications. For example, you can use the --silent parameter to suppress the progress meter and other unnecessary output. You can also use the --data-binary parameter to send data to a server in a binary format. Here's an example of how to use these parameters to display the current temperature in a more readable format:
curl --silent wttr.in/[LOCATION]?0 | grep -o '°C.*'
The grep command is used to filter the output and display only the line that contains the temperature value. The -o option tells grep to only display the matched portion of the line. In this case, we're using the regular expression '°C.*' to match the temperature value, which is displayed in degrees Celsius. If you prefer to display the temperature in degrees Fahrenheit, you can use the regular expression '°F.*' instead.
In this article, we've shown you how to use the command-line tool curl to display the current temperature value using the popular weather service wttr.in. By using the ?0 parameter, you can display just the current temperature value without having to open a web browser or a dedicated weather app. We've also shown you how to format the output to make it easier to read or to use in other applications. We hope you find this information useful and that it helps you to quickly check the current temperature value whenever you need to.
References
| Title | URL |
|---|---|
| wttr.in | https://wttr.in |
| curl - Command Line Tool for Transferring Data | https://curl.se/ |
| grep - Print lines matching a pattern | https://man7.org/linux/man-pages/man1/grep.1.html |