0

I'm writing a small shell script like this:

curl -X POST --header 'Bearer "$(printf  user:pass | base64)"' 'https://api.com/v1/auth'

To debug I switched to echo:

echo 'Bearer "$(printf  remote-key-sync:2klic-hlqDZPGmqJTwhqVkPubld9ReXAnQSks | base64)"' 

But the result is:

Bearer "$(printf  remote-key-sync:2klic-hlqDZPGmqJTwhqVkPubld9ReXAnQSks | base64)"

How can I update my curl command so that it sends --header 'Bearer myBase64String' with the subshell results inside the single quote?

2
  • Remove the outer single quotes. Otherwise the $() would not be executed by bash. Replace them with double quotes instead.
    – J. Pee
    Commented Aug 7, 2017 at 13:16
  • Your original code is broken for the same reason; the header contains the literal string $(printf user:pass | base64), not the base64-encoded credentials.
    – chepner
    Commented Aug 7, 2017 at 13:19

1 Answer 1

6

Start by refactoring your code.

bearer="Bearer \"$(printf user:pass | base64)\""
curl -X POST --header "$bearer" 'https://api.com/v1/auth'

Now when you go to debug, you don't need to quote the argument(s) again.

echo curl -X POST --header "$bearer" 'https://api.com/v1/auth'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .