Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curl command doesn't work in bash script

I am trying to upload a JSON file into my noSQL database using a bash script, but it doesn't work and I don't understand why.

This is the script :

test='{"evaluation": "none"}'
test="'$test'"
command="curl -XPUT localhost:9200/test/evaluation/$i -d $test"
echo "$command"
$command 

This is the error :

curl -XPUT localhost:9200/test/evaluation/0 -d '{"evaluation": "none"}'
{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}curl: (3) [globbing] unmatched close brace/bracket in column 7

When I do the command given in my command line it works fine though.

What is the error here ? Thank you

like image 843
Chococo35 Avatar asked Aug 03 '26 02:08

Chococo35


1 Answers

Don't store a command in a variable; if you absolutely must have something usable with logging, put the arguments in an array.

test='{"evaluation": "none"}'
args=( -XPUT localhost9200/test/evaluation/"$i" -d "$test" )
echo "curl ${args[*]}"
curl "${args[@]}"
like image 82
chepner Avatar answered Aug 04 '26 16:08

chepner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!