0

I am creating a bash script where I need to pass variable $matchteams & $matchtime into below curl command

curl -X POST \
  'http://5.12.4.7:3000/send' \
  --header 'Accept: */*' \
  --header 'User-Agent: Thunder Client (https://www.thunderclient.com)' \
  --header 'Content-Type: application/json' \
  --data-raw '{
  "token": "abcdjbdifusfus",
  "title": "$matchteams | $matchtime",
  "msg": "hello all",
  "channel": "1021890237204529235"
}'

Can someone please help me

3
  • I see the problem is the old "I used ' and as exactly the reason to do that, now $ expressions aren't expanded"; but in general, it helps actually explaining what the problem is, i.e., saying what happens instead of what you expected. Commented Oct 29, 2022 at 8:30
  • "{ \"token\": \"abcdjbdifusfus\", \"title\": \"$matchteams | $matchtime\", \"msg\": \"hello all\", \"channel\": \"1021890237204529235\" }" Commented Oct 29, 2022 at 12:17
  • See askubuntu.com/a/772065/18306
    – jlliagre
    Commented Oct 29, 2022 at 12:17

1 Answer 1

3

Text inside single quotes is treated as literals:

--data-raw '{
  "token": "abcdjbdifusfus",
  "title": "$matchteams | $matchtime",
  "msg": "hello all",
  "channel": "1021890237204529235"
}'

(The double quotes surrounding your variables are also treated as literals.) In this case you need either to come out of the single quotes so that the variables will get parsed and expanded by the shell, or enclose the entire string in double quotes, escaping literal double quote marks appropriately:

# Swapping between single quote strings and double quote strings
--data-raw '{
  "token": "abcdjbdifusfus",
  "title": "'"$matchteams | $matchtime"'",
  "msg": "hello all",
  "channel": "1021890237204529235"
}'

# Enclosing the entire string in double quotes with escaping as necessary
--data-raw "{
  \"token\": \"abcdjbdifusfus\",
  \"title\": \"$matchteams | $matchtime\",
  \"msg\": \"hello all\",
  \"channel\": \"1021890237204529235\"
}"

Remember that "abc"'def' is expanded by the shell as abcdef so it's quite acceptable to swap quoting style mid-string. On balance I would tend to use the first style.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .