0

I have a shell script foo.sh

curl -i -X GET 'https://example.com/bar' \                                                                                                                      
    -H 'Authorization: Bearer abc123def456'                                                                                                                     

which works fine.

However if I change it to use an argument for the value of bearer token ...

curl -i -X GET 'https://example.com/bar' \                                                                                                                      
    -H 'Authorization: Bearer $1'                                                                                                                               

and invoke it like this ...

$ foo.sh abc123def456   

The curl request returns as unauthorised . I have tried various ways of providing the value to foo.sh. Attempt one ...

$ . ./foo.sh $(python -c "import get_token; print(get_token.get_token())")                                                                                     

... Attempt two ...

$ python -c "import get_token; print(get_token.get_token())" > secret_temp.txt                                                                                 
$ . ./foo.sh $(cat secret_temp.txt)                                                                                                                            

... but the result is the same.

2
  • 1
    Variables are not expanded inside single quotes, only double quotes.
    – Barmar
    Commented May 30, 2024 at 22:28
  • 1
    Separately, after you fix the quotes so $1 is in a double-quoted context, note that your expansions ($(cat ...), $(python ...)) also should be in a double-quoted context. When you use ./yourscript $(foo) you have no control over how many arguments the output of foo gets split and globbed into; to make sure it's exactly one, you need to use ./yourscript "$(foo)" with the quotes. Same applies for variables in general: ./yourscript "$foo" not ./yourscript $foo. Commented May 31, 2024 at 0:09

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.