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.
$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 offoo
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
.