0

I think I am missing something simple here, but here goes.

Thsi works from the command line with no problem, it give me the required output.

curl -X GET \
     -H "X-Auth-Email: REDACTED" \
     -H "X-Auth-Key: REDACTED" \
     "https://api.cloudflare.com/client/v4/zones/aa5ac150d414359642d85f1aa434e5db/filters" | jq -r '.result[0] .id'

However when I try to use it in a bash script, I get no output at all

FWRID="$(curl -X GET \
     -H "X-Auth-Email: REDACTED" \
     -H "X-Auth-Key: REDACTED" \
     "https://api.cloudflare.com/client/v4/zones/aa5ac150d414359642d85f1aa434e5db/filters")" | jq -r '.result[0] .id'

echo "$FWRID"

Any help would be most appreciated.

1 Answer 1

2

In your second command, you end the command substitution before the pipeline to jq. That call to jq is what parses out the value you want, so it should be part of the command substitution:

FWRID=$(
    curl -X GET \
        -H 'X-Auth-Email: REDACTED' \
        -H 'X-Auth-Key: REDACTED'   \
        'https://api.cloudflare.com/client/v4/zones/aaxxx/filters' |
    jq -r '.result[0] .id'
)

In your original command, where you leave jq outside of the command substitution, jq would read the output of the assignment to the FWID variable and produce some result from that. Since the assignment doesn't produce any output, it does not do anything interesting at all.

Furthermore, since the assignment in your original command is part of a pipeline, it runs in a subshell, so the value $FWID is empty (or at least unchanged) in the call to echo later.

1
  • Many thanks. Works perfectly now. Commented Dec 4, 2019 at 13:33

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.