Search code examples
bashjenkinsenvironment-variablesquoting

Using environment variables within a Bash script in Jenkins


I know that there are a few questions with answers on how to do this, however I still can't seem to get my scenario to work when wrapping my variable in double quotes.

I am executing a curl request to obtain a valid crumb in Jenkins so that I can then execute a job via a POST request.

So at the moment I get response like Password Invalid as the variable ${USER_TOKEN} is not recognised

echo "The USER TOKEN is " ${USER_TOKEN} # This outputs 123456789 for example
CRUMB=$(curl -s 'http://jenkins:${USER_TOKEN}@localhost:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField, ":",//crumb)')

If I hardcode the USER_TOKEN then this will work but I obviously wanted to avoid that.

How can I execute this curl command and pass in the USER_TOKEN?

Updates

If I surround ${USER_TOKEN} with double quotes I still get the same error.

CRUMB=$(curl -s 'http://jenkins:"${USER_TOKEN}"@localhost:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField, ":",//crumb)')

And if I surround the curl request with double quotes I get

Invalid Xpath expression, contact(//crumbRequestField,:,//crumb)
Unexpected ':'

Solution

  • You need to double quote a string that contains double quotes. This is one way:

    CRUMB=$(curl -s "http://jenkins:${USER_TOKEN}@localhost:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField, "'":"'",//crumb)")