engroteng
engroteng

Reputation: 3

Bash JSON: Add Var

I'm trying to use curl command to perform a POST request. The format of the content is the message I am POSTing is JSON. In the example below, I don’t get how to add the $* part, which should be all parameters you add when you start the .sh file.

curl -X POST --data-urlencode 'payload={"channel": "#serverstatus", "username": "BananaStatus", "text": "${*}", "icon_emoji": ":banana:"}' https://hooks.slack.com/services/

When I run that, I just receive the ${*} as text.

Upvotes: 0

Views: 41

Answers (2)

Lucas Piske
Lucas Piske

Reputation: 370

When using variables in the middle of strings you should use double quotes.

So, use:

"payload={\"channel\": \"#serverstatus\", \"username\": \"BananaStatus\", \"text\": \"${*}\", \"icon_emoji\": \":banana:\"}"

Instead of:

'payload={"channel": "#serverstatus", "username": "BananaStatus", "text": "${*}", "icon_emoji": ":banana:"}'

Upvotes: 1

sligor
sligor

Reputation: 69

bash don't expand variables between ' ' (single quotes)

you have to use double quote (" ")

try:

'...any text..'"${*}"'...other text..'

it gives whith your example:

curl -X POST --data-urlencode 'payload={"channel": "#serverstatus", "username": "BananaStatus", "text": "'"${*}"'", "icon_emoji": ":banana:"}' https://hooks.slack.com/services/  

Upvotes: 2

Related Questions