I am trying to write a bash script to update certain nodes in my repository. I wrote below script, but it does not seem to be working when I use variables inside curl. Below is the code. I tried all possible combinations using "" inside curl statement to resolve the variable. But it does not seem to update the nodes. (I don't get any error when running the script).

I echo'ed the curl line like:

echo "curl --user admin:admin "$final_add" http://localhost:4502"$a""

and placed its output in the script, then the script runs fine and updated the node.

Can anyone provide me some guidance on why cant i update the nodes using the variables in curl.

Code Sample Below


echo "-------------------------------------------------------------------------------------------------------------------"
echo "Script to set tags"
echo "-------------------------------------------------------------------------------------------------------------------"

if [ true ]
    echo "**firing curl command for tags2**"


    for key in "${!ary[@]}"; do tags_paths+="-Ftags2=${ary[$key]} "; done 

    final_paths=$(echo $tags_paths | sed "s|[2],]||g")

    final_add="-Ftags2@TypeHint=\"String[]\" ${final_paths//[[[\[\]]/}"

    #have tried this without quotes too --eg : (curl --user admin:admin  $final_add http://localhost:4502$a) it too didnt work
    curl --user admin:admin  "$final_add" http://localhost:4502"$a"
  • 1
    With the script as written, the content of "$final_add" is passed to curl as one argument, containing -Ftags2@TypeHint="String[]" -Ftags2=/content/cq:tags/sales-stage/pre-sale -Ftags2=/content/cq:tags/sales-stage/special-offer. Is this what you want? – nohillside Nov 4 '18 at 14:22
  • I need a command like this: (curl --user admin:admin -Ftags3@TypeHint="String[]" -Ftags3=/content/cq:tags/sales-stage/pre-sale -Ftags3=/content/cq:tags/sales-stage/special-offer localhost:4502/content/something/events/whats-on/…) does my $final_add ned to be changed for this? – Komatsu Kai Nov 4 '18 at 14:36
  • You can use curl -v option to know the network request details. – 林果皞 Nov 4 '18 at 15:26

Your issue is mainly with the -F flags in the string $final_paths. It is being passed a a single argument to curl. The solution is not to un-quote the variable expansion to rely on the shell splitting the string properly.

When you have a list of things that you need to pass to a program as separate items, use an array:




curl_opts=( --user "admin:admin" --form "tags3@TypeHint=String[]" )

for tag_path in "${tag_paths[@]}"; do
    curl_opts+=( --form "tags2=$tag_path" )

curl "${curl_opts[@]}" "$url"

Here, we put the options to pass to curl in the array curl_opts. We initiate this array with the things we know will always be there and then add the tag path options by iterating over the array tag_paths. The double-quoted expansion "${curl_opts[@]}" at the end will expand to all elements of the curl_opts array, with each element individually quoted.

I've also chosen to build the complete URL at the start as it is static, and I'm using the long option for curl as this is a script and we can afford to be a bit more verbose (for the sake of readability).

Doing it this way, the quoting becomes intuitive and you don't need to bother with parsing comma-separated lists, with escaping special characters, or setting IFS to some non-default value.

The same script, but for /bin/sh:



set -- \
    '/content/cq:tags/sales-stage/pre-sale' \

for tag_path do
    set -- "$@" --form "tags2=$tag_path"

set -- --user "admin:admin" --form "tags3@TypeHint=String[]" "$@"

curl "$@" "$url"

Here, we are restricted to using only one array, $@. Elements are set in this array with set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.