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?

  • Remove the outer single quotes. Otherwise the $() would not be executed by bash. Replace them with double quotes instead.
    – J. Pee
    Aug 7 '17 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
    Aug 7 '17 at 13:19

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'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.