2

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?

Inian
  • 80,270
  • 14
  • 142
  • 161
Nikk
  • 7,384
  • 8
  • 44
  • 90
  • 2
    Use http://shellcheck.net to catch the errors in your script. – chepner Apr 05 '18 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 – Inian Apr 05 '18 at 11:49
  • 1
    plenty of issues.. there shouldn't be spaces around `=` ... `$info` inside single quote - won't get expanded.. see https://mywiki.wooledge.org/Quotes – Sundeep Apr 05 '18 at 11:50
  • @Inian for single/double quote --> https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash – Sundeep Apr 05 '18 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 – Inian Apr 05 '18 at 11:53
  • https://stackoverflow.com/questions/2268104/command-not-found-error-in-bash-variable-assignment could also qualify, although in this case `info` actually *is* a command, which masks the error in the assignment. – chepner Apr 05 '18 at 11:54

1 Answers1

13

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"'" }]}'
Inian
  • 80,270
  • 14
  • 142
  • 161