0

This is a small command line script that is used to post a json body in an http server. I am finding difficulties to pass the first command line argument $1 to the json body.

#!/bin/bash
curl  -X POST -d '{  "game": 16, "id": $(($1)) }' http://localhost:10000/

The command does not fail, however the http body contains exactly

{ "game": 16, "id": $(($1)) }

I want to run the script ./script 123 and send the json body

{  "game": 16, "id": 3 }

How can I do this using bash?

2
  • Yes, everything is needed. It is going to be a number actually, eg 12345 so I need all chars Commented Sep 12, 2017 at 11:51
  • In general, you should not be creating JSON using string interpolation, because the contents of $1 may need to be correctly escaped to produce valid JSON. Something like $(jq --arg val "$1" '{game: 16, id: $val}') is more appropriate. Commented Sep 12, 2017 at 14:32

2 Answers 2

2

You can also use single quotes so you don't have to escape double quotes like this:

#!/bin/bash
curl  -X POST -d '{  "game": 16, "id": '$1' }' http://localhost:10000/
Sign up to request clarification or add additional context in comments.

Comments

1

Using single quotes will print literal characters. You need to use double quotes for string interpolation. Try:

curl  -X POST -d "{  \"game\": 16, \"id\": $1 }" http://localhost:10000/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.