1

I have an argument to a bash script which could be a string with spaces. How can I use this within a string concatenation such as below:

#!/usr/bin/env bash

curl -X POST -H 'Content-type: application/json' \
--data '{"text":"'$1'"}' https://hooks.slack.com/services/<rest of URL secret!>

From cmd line if I do:

$./myscript.sh oneword

It works perfectly fine. But if I pass a string with spaces:

$./myscript.sh "two words"

curl: (3) [globbing] unmatched close brace/bracket in column 5
invalid_payload%  

I have tried all sorts of quoting to concatenate it correctly, but to no avail. Thanks

Zuriar
  • 10,078
  • 18
  • 51
  • 89

1 Answers1

1

You need to quote $1 as well in your JSON payload:

curl -X POST -H 'Content-type: application/json' \
--data '{"text":"'"$1"'"}' https://hooks.slack.com/services/<rest of URL secret!>
anubhava
  • 713,503
  • 59
  • 514
  • 593