2

I'm currently working on a bash script and I want to save a specific part of a curl-command output to a variable.

This is my curl command with some test parameters: curl -k -I --header "Accept: application/*;version=34.0" --header "Authorization: Basic testXXX1" --request POST https://test.io/api/sessions

The output of the abovementioned curl command:

Date: Mon, 19 Apr 2021 12:13:48 GMT
REQUEST-ID: test1
ACCESS-TOKEN: test1.testXXX.testXX-testXXXX_testXXX_testXXXX-testXX_NR_TESTXX_test_nnndnndnddn_jdjdjdjd_jbjndinfjenkndkn-nnjjnj
TOKEN-TYPE: Bearer
authorization: test1
Content-Type: application/test;version=34.0
REQUEST-EXECUTION-TIME: 12
Cache-Control: no-store, must-revalidate
Vary: Accept-Encoding, User-Agent
Connection: close
Set-Cookie: de.test

Now I just want to catch the value of 'Access-Token' and store it to a variable, so if I echo that variable or want to use it for further commands it'll only return the value of my access token (test1.testXXX.testXX-...).

I'd be glad for any help.

Jaw. M.
  • 139
  • 2
  • 15

1 Answers1

2

Multiple options, if you're only after the header:

ACCESS-TOKEN=`curl -qs -k -I --header "Accept: application/*;version=34.0" \
--header "Authorization: Basic testXXX1" \
--request POST https://test.io/api/sessions | grep ACCESS-TOKEN: | sed 's/ACCESS-TOKEN: //g' | tr -d '\r'`
wowbagger
  • 590
  • 5
  • 15
  • 3
    The result is likely to include a carriage return at the end of each line, so you might need to add something like `| tr -d '\r'` to the pipeline to remove it. – Gordon Davisson Apr 19 '21 at 18:52