0
0
Bash Scriptingscripting~15 mins

HTTP requests with curl in scripts in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
HTTP requests with curl in scripts
📖 Scenario: You want to automate checking the status of a website using a script. This helps you know if the website is up or down without opening a browser.
🎯 Goal: Build a bash script that uses curl to send an HTTP request to a website and shows the HTTP status code returned.
📋 What You'll Learn
Create a variable with the website URL
Create a variable to store curl options
Use curl with the variables to get the HTTP status code
Print the HTTP status code
💡 Why This Matters
🌍 Real World
Automating website health checks helps system administrators and developers monitor if websites are online without manual checking.
💼 Career
Knowing how to use curl in scripts is useful for roles in IT support, DevOps, and automation testing.
Progress0 / 4 steps
1
Set the website URL
Create a variable called url and set it to the string "https://example.com".
Bash Scripting
Need a hint?

Use url="https://example.com" to store the website address.

2
Set curl options
Create a variable called curl_opts and set it to the string "-s -o /dev/null -w '%{http_code}'" to make curl silent, discard output, and show only the HTTP status code.
Bash Scripting
Need a hint?

Use curl_opts='-s -o /dev/null -w "%{http_code}"' to set curl options.

3
Get HTTP status code using curl
Create a variable called status_code and set it to the output of running curl with $curl_opts and $url.
Bash Scripting
Need a hint?

Use status_code=$(curl $curl_opts "$url") to run curl and save the status code.

4
Print the HTTP status code
Write a echo command to print the text "HTTP status code: " followed by the value of status_code.
Bash Scripting
Need a hint?

Use echo "HTTP status code: $status_code" to show the result.