Back to: Linux
Introduction
Curl command or simply CURL is a command-line utility that allows you to transfer data from or to a server using different protocols such as HTTP, FTP, IMAP, POP3, SMTP, etc. In a shell script, curl can be used to automate the process of fetching data from remote servers and storing it locally, or sending data to a remote server.
Syntax
The basic syntax for using curl command in a shell script is as follows:
curl [options] [URL]
Here, [options]
are the various command-line options available for curl, and [URL]
is the URL of the remote server to fetch or send data to.
Options: Curl has a large number of options available for different use cases. Here are a few commonly used options:
-o
: Specifies the name of the file to which the data should be saved. For example,curl -o data.txt https://example.com/data.txt
will save the contents ofhttps://example.com/data.txt
to a file nameddata.txt
.-O
: Saves the output with the same name as the remote file. For example,curl -O https://example.com/data.txt
will save the contents ofhttps://example.com/data.txt
to a file nameddata.txt
.-d
: Sends data to the remote server using HTTP POST. For example,curl -d "param1=value1¶m2=value2" https://example.com/post
will send the data “param1=value1¶m2=value2” tohttps://example.com/post
.-H
: Sets a header for the request. For example,curl -H "Authorization: Bearer TOKEN" https://example.com/api
will set the “Authorization” header to “Bearer TOKEN” for the request.-s
: Silent mode. Disables progress and error messages.-u
: Sets the username and password for the request. For example,curl -u username:password https://example.com/api
will set the username to “username” and the password to “password” for the request.
Examples
- Downloading a file:
#!/bin/bash
curl -o data.txt https://example.com/data.txt
This script will download the contents of https://example.com/data.txt
and save it to a file named data.txt
.
- Uploading a file:
#!/bin/bash
curl -F "file=@/path/to/local/file.txt" https://example.com/upload
This script will upload the file /path/to/local/file.txt
to https://example.com/upload
.
- Sending data using HTTP POST:
#!/bin/bash
curl -d "param1=value1¶m2=value2" https://example.com/post
This script will send the data “param1=value1¶m2=value2” to https://example.com/post
using HTTP POST.
An example code snippet that uses curl in a shell script to download a file from a remote server
#!/bin/bash
# Set the URL of the file to download
URL="https://example.com/data.txt"
# Set the output file name
OUTPUT="data.txt"
# Use curl to download the file and save it to the specified output file
curl -o $OUTPUT $URL
# Check if the download was successful
if [ $? -eq 0 ]; then
echo "Download completed successfully"
else
echo "Download failed"
fi
In this example, the script sets the URL of the file to download and the name of the output file. It then uses the curl
command with the -o
option to download the file and save it to the specified output file.
The script then checks the exit status of the curl
command using the $?
variable. If the exit status is 0
, it prints a message indicating that the download was successful. If the exit status is not 0
, it prints a message indicating that the download failed.
Example to get and use Authentication Token
A code snippet that uses curl in a shell script to obtain an authentication token from a server and use it in subsequent requests:
#!/bin/bash
# Set the URL of the authentication endpoint
AUTH_URL="https://example.com/auth"
# Set the credentials to use for authentication
USERNAME="user"
PASSWORD="pass"
# Use curl to authenticate and obtain a token
TOKEN=$(curl -s -X POST -H "Content-Type: application/json" -d '{"username": "'"$USERNAME"'", "password": "'"$PASSWORD"'"}' $AUTH_URL | jq -r '.token')
# Check if the token was obtained successfully
if [ -z "$TOKEN" ]; then
echo "Failed to obtain authentication token"
exit 1
fi
# Set the URL of the resource to access
RESOURCE_URL="https://example.com/resource"
# Use curl with the token to access the resource
curl -s -H "Authorization: Bearer $TOKEN" $RESOURCE_URL
In this example, the script first sets the URL of the authentication endpoint and the credentials to use for authentication. It then uses curl to send a POST request to the authentication endpoint with the specified credentials, and use jq
to extract the token from the JSON response.
The script then checks if the token was obtained successfully, and if so, sets the URL of the resource to access. It then uses curl with the -H
option to set the Authorization
header to Bearer $TOKEN
, and sends a request to access the resource.
Also, see the example code shell-scripting-examples in our GitHub repository. See complete examples in our GitHub repositories.
Follow us on social media
Follow Author