0

I'm trying to get both the HTTP code and the output of a curl command as part of a shell script, but in turn I'm trying to set both of them as variables. My code (which uses the pushbullet API) looks like this:

CURL_OUTPUT="$(exec 3>&1; \
  HTTP_CODE="$(curl -s -S \
    -w "%{http_code}" -o >(cat >&3) \
    --header 'Authorization: Bearer '"$ACCT_TOKEN" \
    -X POST https://api.pushbullet.com/v2/pushes \
    --header 'Content-Type: application/json' \
    --data-binary "$JSON")" \
)"

which should theoretically set $CURL_OUTPUT to the JSON returned by curl and $HTTP_CODE to the status code I get. Instead, I get only $CURL_OUTPUT; $HTTP_CODE is empty.

However, if I don't do the outer nest, like so:

exec 3>&1; \
  HTTP_CODE="$(curl -s -S \
    -w "%{http_code}" -o >(cat >&3) \
    --header 'Authorization: Bearer '"$ACCT_TOKEN" \
    -X POST https://api.pushbullet.com/v2/pushes \
    --header 'Content-Type: application/json' \
    --data-binary "$JSON")" \

The command works as expected; I get the JSON redirected to stdout, and the status code ends up in $HTTP_CODE.

So is it possible to get both outputs? Can I assign variables within a command substitution line?

Community
  • 1
  • 1
Roger Filmyer
  • 676
  • 1
  • 8
  • 24
  • Each `$()` is a sub-shell, so no, you can't assign inside one and view it outside. – Etan Reisner Mar 12 '15 at 02:56
  • If that's the case, then [going by this](http://stackoverflow.com/questions/13806626/capture-both-stdout-and-stderr-in-bash) my only options are a temporary file or a named pipe? – Roger Filmyer Mar 12 '15 at 02:58
  • As far as I'm aware, yes. – Etan Reisner Mar 12 '15 at 02:58
  • @RogerFilmyer: Out of curiosity, isn't it possible to just output both the body and the -w message to stdout, so they can be captured, and then separate them afterwards? – rici Mar 12 '15 at 04:54

0 Answers0