Michael Agarenzo
Michael Agarenzo

Reputation: 239

Replacing a string with a variable in a cURL command

I am trying to automate a GroupMe bot as simply as possible. An easy way to send a message from the command line is to use the following command:

curl -d '{"text" : "Your message here", "bot_id" : "this_is_a_secret_string"}' https://api.groupme.com/v3/bots/post

In a Shell script, I would like to replace "Your message here" with var, in which var is being set to the output from a different command. Is this possible?

Things I have replaced "Your message here" with that did not work:

var
$var
(var)
$(var)
{var}
${var}

Anything put within double quotes ("") is treated as a String, so did not try much in those regards.

Upvotes: 1

Views: 8939

Answers (1)

jeberle
jeberle

Reputation: 758

The var will not be evaluated because it's w/in single quotes. One way around this is to just smash 3 strings together:

curl -d '{"text" : "'"$var"'Your message here", "bot_id" : "this_is_a_secret_string"}' https://api.groupme.com/v3/bots/post
  • string 1: '{"text" : "'
  • string 2: "$var"
  • string 3: 'Your message here", "bot_id" : "this_is_a_secret_string"}'

NOTE: this will only work if the contents of var are very simple. The expanded string must still be a valid JSON string.

Upvotes: 9

Related Questions