'Why does curl see several parameters? [duplicate]

I want to send a message to a slack app in a script:

...
declare -a models=("TR10"
                   "TR100"
                   "TR1000")

for i in "${models[@]}"
do
   #Send Message
   if [ $use_slack_app ]
   then
     message="Starting model $i ($c of $lenght)!"
     data=''\''{"text":"'"$message"'"}'''\'
     echo $data
     curl -X POST -H 'Content-type: application/json' --data $data $slack_app_url
     echo "curl -X POST -H 'Content-type: application/json' --data $data $slack_app_url"
   fi

   #Counter
   ((c=c+1))
done  
...

return of echo $data:

'{"text":"Starting model TR10 (1 of 3)!"}'

curl message:

curl: (3) unmatched close brace/bracket in URL position 5:
3)!"}'
    ^
curl: (6) Could not resolve host: model
curl: (6) Could not resolve host: TR10
curl: (6) Could not resolve host: (1
curl: (6) Could not resolve host: of
invalid_payload

It looks like it sees the string as several parameters, but I don't understand why. If I echo the line out, and run it in the terminal it works. I don't have much experience with bash scripting, so I don't understand what the problem here is.



Solution 1:[1]

  1. Quote the variable expansions to ensure values with spaces are sent as one word: "$var" vs. $var.

  2. Get rid of the single quotes (''\') surrounding $data.

data='{"text":"'"$message"'"}'
curl -X POST -H 'Content-Type: application/json' --data "$data" "$slack_app_url"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 John Kugelman