0

As you all know tokens can be very long strings and become a hassle to copy and past over and over.

How can I store the token string as a variable and call it when I need it in cURL

example

token: "ABCDefG"

I want to be able to call something like:

curl -L --silent --header "Authorization: GoogleLogin auth=${token}"
dHumphrey
  • 307
  • 2
  • 7
  • 24

2 Answers2

1

Inside a bash script, can't you put the token in a variable, like this:

#!/bin/bash

token="ABCDef"
curl -L --silent --header "Authorization: GoogleLogin auth=$token"

Now in the bash script whenever you need to use the token, you just need to use the variable "$token" (remember to enclose the variable in double quotes).

Or you could set an environment variable:

export token=ABCDef

but it's not an elegant solution

condorwasabi
  • 616
  • 6
  • 19
  • You say" (remember to enclose the variable in double quotes)." but you don't do that in the curl example you gave. – Frak Sep 10 '19 at 17:38
1

You could store the token in an array

tokens=("ABCDeF" "ASDFGh")

Then when you want to call them, use

curl -L --silent --header "Authorization: GoogleLogin auth=${tokens[0]}"

And if you want to add a token you can

tokens+=("qwerty")
Lattis
  • 73
  • 1
  • 5
  • That sounds like it will work, thanks! Where would the array go in relevance of the script? – dHumphrey Jul 07 '14 at 17:28
  • The array can go anywhere as long as it is set before the curl is called, otherwise there will be no value for ${tokens[0]}. – Lattis Jul 07 '14 at 17:31
  • a different token, in case there were more than one tokens that you needed to store – Lattis Jul 08 '14 at 21:51