I am trying to write a script which will execute two curl requests in bash. This is my code:


header1="Accept: application/json"
header2="Content-Type: application/json"

function start_player {
        curl --verbose -H \"${header1}\" -H \"${header2}\" -X PUT -d '{\"id\": \"$stp\"}' ${start_url}

function stop_player {
        curl -X PUT $stop_url


The stop_player function works without a problem, but the first function doesn't work. I just want to execute the following CURL request: curl --verbose -H "Accept: application/json" -H "Content-Type: application/json" -X PUT -d '{"id": "c67664db-bef7-4f3e-903f-0be43cb1e8f6"}' If I echo the start_player function, the output is exactly as it should be, but if I execute the start_player function I got an error: Could not resolve host: application. I think it is because bash is splitting the headers but why it works fine with echo, but not in bash?

1 Answer 1


You wrote:

curl --verbose -H \"${header1}\" -H \"${header2}\" ...

but it looks like you really want:

curl --verbose -H "${header1}" -H "${header2}" ...

With the values you have set for header1 and header2, the former would cause curl to receive as parameters --verbose, -H, "Accept:, application/json", -H, "Content-Type:, and application/json", whereas you really want each header value as its own token, which the unescaped double quotes will provide.

Additionally, I see you pass -d '{\"id\": \"$stp\"}'. You probably want -d "{\"id\": \"$stp\"}" there.

As to your question about why whings appear to work fine within echo, "but not in bash", well, things did not actually work OK with echo, it's just that it makes that fact hard to see.


$ h1='Accept: foo'; h2='Content-Type: bar'

## Looks good, is actually wrong:
$ echo curl -H \"$h1\" -H \"$h2\"
curl -H "Accept: foo" -H "Content-Type: bar"

## If we ask printf to print one parameter per line:
$ printf '%s\n' curl -H \"$h1\" -H \"$h2\"


## Looks different from the bash command, is actually right:
$ echo curl -H "$h1" -H "$h2"
curl -H Accept: foo -H Content-Type: bar

## This is more obvious if we ask printf to print one parameter per line:
$ printf '%s\n' curl -H "$h1" -H "$h2"
Accept: foo
Content-Type: bar
  • This won't work it will transmit the following: curl --verbose -H Accept: application/json -H Content-Type: application/json without the quotations around. Mar 1, 2018 at 16:21
  • I suggest you actually give it a try.
    – dhag
    Mar 1, 2018 at 16:22
  • I did, it doesn't work. I got error 500 Internal Error. Mar 1, 2018 at 16:22
  • Thanks for mentioning the new error message! This could be due to the broken JSON you were passing.
    – dhag
    Mar 1, 2018 at 16:26
  • actually the problem was in the JSON request and I needed to exchange the single with double quotes, now it works, Thanks a lot @dhag!!! Mar 1, 2018 at 16:28

You must log in to answer this question.

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