I'm trying to run some REST API call via curl, but I'm facing some issue when it comes to generating that curl command using bash.
Please see my example code:
#!/bin/bash
CURL_ARGS="-H \"Content-Type: application/json\" --data @somefile.json"
# further variables are set here as well
set -o xtrace # print command before execution
curl -k -X ${PROTOCOL} -u "${CICD_USER}:${CICD_PW}" ${CURL_ARGS} ${URL} -v
Output of xtrace:
+ curl -k -X POST -u redacted:redacted -H '"Content-Type:' 'application/json"' --data @somefile.json https://redacted/rest/v1/group/import -v
The curl command obviously fails because ${CURL_ARGS}
is not resolved correctly.
I'm confused about the expansion of ${CURL_ARGS}
. Why is the string expanded to -H '"Content-Type:' 'application/json"'
?
What I'd expect is to generate the curl command like this:
curl -k -X POST -u redacted:redacted -H "Content-Type: application/json" --data @somefile.json https://redacted/rest/v1/group/import -v