How to Use Curl in a Shell Script

How to use curl in a shell script?

#!/bin/bash                                                                                                                                                                                     
CURL='/usr/bin/curl'
RVMHTTP="https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer"
CURLARGS="-f -s -S -k"

# you can store the result in a variable
raw="$($CURL $CURLARGS $RVMHTTP)"

# or you can redirect it into a file:
$CURL $CURLARGS $RVMHTTP > /tmp/rvm-installer

or:

Execute bash script from URL

Execute bash script from URL

source <(curl -s http://mywebsite.example/myscript.txt)

ought to do it. Alternately, leave off the initial redirection on yours, which is redirecting standard input; bash takes a filename to execute just fine without redirection, and <(command) syntax provides a path.

bash <(curl -s http://mywebsite.example/myscript.txt)

It may be clearer if you look at the output of echo <(cat /dev/null)

How to run curl in bash script and keep the result in a variable

Your problem here is that all you are doing on the first command is just setting cmd to equal a string.

Try using $(...) to execute the actual command like so:

cmd=$(curl -v -H "A: B" http://stackoverflow.com)

The result of this will be the actual output from with curl request.

This has been answered many-times see here for example Set variable to result of terminal command (Bash)

Use CURL Command to download remote files in Shell scripting

Using a valid url in my tests seemed to have no issues with this curl request. I also tried using with and without wrapping the url in a string like you are along with using brackets.

url="https://cdn.sstatic.net/Sites/stackoverflow/img/sprites.svg?v=1b3cdae197be"

# all work fine
curl -O -J $url
curl -O -J "$url"
curl -O -J "{$url}"

My best answer is that your $url_final variable is undefined, empty, or malformed.

Bash script curl

#!/bin/bash

read -p 'Username: ' uservar
read -p 'Password: ' passvar

cacheVariable1="\"Content-Type:application/json"\"
cacheVariable2="\"Cache-Control:no-cache"\"
parametersVariable="'{\"username\":\"$uservar\",\"password\":\"$passvar\"}'"
echo $parametersVariable
echo $cacheVariable1 $cacheVariable2
websiteVariable="https://example.com/session"

echo $websiteVariable

entireURL="curl -X POST -H "$cacheVariable1" -H "$cacheVariable2" -d "$parametersVariable" "$websiteVariable""

echo "Entire URL IS: $entireURL"

result=`$entireURL`

eval $entireURL

This works perfect !

How to run a curl command with headers using shell script

The command you want to execute is something like:

curl -X POST --header "$H1" --header "$H2" --header "$H3" -d @s_100mb.xml "$URL"

(I'll use -H instead of --header as it's shorter.)

The easiest way to do it is

response=$(curl -X POST -H "$H1" -H "$H2" -H "$H3" -d @s_100mb.xml "$URL")

The problem with your solution is that you're not delimiting the header values at all:

curl -X POST -H $H1

If the content of H1 is foo: bar, then this will expand to

curl -X POST -H foo bar

and that will be interpreted as

curl -X POST -H (foo:) bar

(using ( and ) only to illustrate precedence, nothing like this works in shell), i.e. bar will be taken to be the first positional argument, which happens to be the hostname, leading to strange errors you see.

What you want is

curl -X POST -H (foo: bar)

which can be achieved by properly wrapping the expansions in quotes as illustrated above.

Also, you should prefer $(cmd) to `cmd`.

As a final piece of advice, if you're learning how to use the shell, it may be wise avoiding multiple expansions, i.e. don't store your command in a CMD variable to $($CMD) it later, as that leads to multiple expansions in multiple places (first where CMD was assigned, second when CMD was expanded in the $(...) sub-shell), making it hard to understand what the heck is going on.

Run curl from .sh script with defined Content-Type

Run the following: (delete the space after Content-Type)

#!/bin/bash
CT="Content-Type:application/json"

TEST="curl http://127.0.0.1 -H $CT"
echo $TEST

RESPONSE=`$TEST`
echo $RESPONSE

Curl in Shell Script Potentially Failing in Headers Section

I believe the issue is in the curl options not being set in separate variables, so the below should resolve the errors:

#!/bin/bash

CURL="/usr/bin/curl"
CURLARGS="-X GET"
BITHTTP="https://api.bitwarden.com/public/members"
header="-H"
accept="accept:text/plain"
auth="Authorization:Bearer XXXX"

$CURL $CURLARGS $BITHTTP $header "$accept" $header "$auth" > /tmp/bitmembers

This answer seems like a good approach to send variables as options to the main curl command. Example:

CURL_OPTIONS=( -X GET "$BITHTTP" -H "$accept" -H "$auth" )
$CURL "${CURL_OPTIONS[@]}"


Related Topics



Leave a reply



Submit