3

I have the script below...

And I cannot seem to get the variables to work

#!/bin/bash

info = 'Help...?'    
object='{"attachments": [{"title": "ti1","text": $info }]}'    
curl -X POST -H 'Content-type: application/json' --data '$object' https://hooks.slack.com/services/xxxx

exit 0

Even --data '$object' doesn't work without $info...as Slack API couldn't read my request.

How do I fix this?

6
  • 2
    Use shellcheck.net to catch the errors in your script. Commented Apr 5, 2018 at 11:48
  • @chepner: Can you find a proper dupe for this? I can't seem to find one in SO, only in Unix.SE Commented Apr 5, 2018 at 11:49
  • 1
    plenty of issues.. there shouldn't be spaces around = ... $info inside single quote - won't get expanded.. see mywiki.wooledge.org/Quotes Commented Apr 5, 2018 at 11:50
  • @Inian for single/double quote --> stackoverflow.com/questions/6697753/… Commented Apr 5, 2018 at 11:51
  • @Sundeep Thanks for a long time I was tentative to use that link, I wanted to dupe with a definitive problem stating that single quotes won't expand the variable and double-quotes are needed. The one you shared is more detailed than needed Commented Apr 5, 2018 at 11:53

1 Answer 1

15

Use double-quotes when passing shell variables and remove extra spaces in variable assignments.

curl -X POST -H 'Content-type: application/json' --data "$object"
#                                                       ^^^^^^^^^^

Use nested quotes to preserve the value inside JSON syntax

info='Help...?'
object='{"attachments": [{"title": "ti1","text": "'"$info"'" }]}'
Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't appear to work for me. it keeps responding that the json data is invalid, but if I send the json data without the variable it is fine. the variable contains only alpha-numeric characters and spaces.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.