0
0
Bash Scriptingscripting~10 mins

HTTP requests with curl in scripts in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - HTTP requests with curl in scripts
Start script
Prepare curl command
Execute curl command
Receive HTTP response
Process or display response
End script
The script starts, prepares a curl command to make an HTTP request, executes it, receives the response, then processes or shows the response before ending.
Execution Sample
Bash Scripting
response=$(curl -s https://api.github.com)
echo "$response"
This script sends a silent HTTP GET request to GitHub API and prints the response.
Execution Table
StepActionCommand/ExpressionResult/Output
1Prepare curl commandcurl -s https://api.github.comCommand ready to run
2Execute curl commandcurl -s https://api.github.comHTTP response JSON received
3Store responseresponse=$(curl -s https://api.github.com)Variable 'response' holds JSON text
4Print responseecho "$response"JSON text printed to terminal
5End scriptN/AScript finishes execution
💡 Script ends after printing the HTTP response stored in 'response' variable.
Variable Tracker
VariableStartAfter Step 3Final
responseemptyJSON text from GitHub APIJSON text from GitHub API
Key Moments - 3 Insights
Why do we use the -s option with curl in the script?
The -s option makes curl silent, so it does not show progress or errors, only the response. This keeps the output clean as shown in execution_table step 2.
How does the script store the HTTP response in a variable?
Using command substitution $(...), the output of curl is captured and assigned to the variable 'response' as shown in execution_table step 3.
What happens if we omit the quotes around "$response" in echo?
Without quotes, the JSON response might be split or misformatted when printed. Quoting preserves the exact response text, as shown in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does the variable 'response' contain after step 3?
AThe HTTP response JSON text
BThe curl command itself
CAn empty string
DAn error message
💡 Hint
Check the 'Result/Output' column in step 3 of the execution_table.
At which step does the script print the HTTP response to the terminal?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for the 'Print response' action in the execution_table.
If we remove the -s option from curl, what changes in the output?
AThe response will be empty
BCurl will not send the HTTP request
CCurl shows progress and error messages along with the response
DThe script will fail to run
💡 Hint
Refer to the key moment about the -s option and step 2 in the execution_table.
Concept Snapshot
curl command in scripts:
- Use curl to send HTTP requests
- -s option makes curl silent (no progress shown)
- Capture output with response=$(curl ...)
- Use quotes when printing variables to keep formatting
- Useful for automation and API calls
Full Transcript
This script uses curl to send an HTTP GET request silently to a URL. The response is captured into a variable using command substitution. Then the script prints the response exactly as received. The -s option keeps the output clean by hiding progress info. Quoting the variable when printing preserves the response format. The script ends after printing the response.