0

I am trying read input to a variable then use this variable in a REST API call data field, but I am unable to escape the variable properly inside quotes.

I have tried backslash before the single quotes, before the double quotes and before both single and double quotes at the same time.

token=$(curl $VAULT_ADDR/v1/auth/ldap/login/$username -d '{"password": "$password"}' -k | jq '.auth.client_token')

echo token

As of now it reads $password as a string. Can't authenticate because the password is wrong then token equals null. I am trying to use the value of variable $password inside double quotes since the data field is supposed to be in JSON-format.

password=hunter2

'{"password": "$password"}' becomes

'{"password": "hunter2"}'

If I hardcode my password it succeeds

NoJohns
  • 3
  • 1

1 Answers1

1
'{"password": "'$password'"}'

?

Šerg
  • 793
  • 5
  • 18
  • This has the unfortunate problem that the shell will perform whitespace tokenization and wildcard expansion on the unquoted value. Always quote your variables unless you know exactly what you are doing. See https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable/27701642 – tripleee Aug 06 '19 at 12:33