Prevent bash from adding single quotes to variable output Prevent bash from adding single quotes to variable output curl curl

Prevent bash from adding single quotes to variable output


A reasonably straightforward solution is to use a bash array to store the four arguments you will want to pass:

CURL_HEADERS=(             '-H' "Authorization: Basic ${AUTH_KEY}"             '-H' 'Content-Type: application/json')curl -s "${CURL_HEADERS[@]}" 'http://www.example.org' > /dev/null

Unlike scalar variables, which are just ordinary strings of ordinary characters no matter how many quotes they might contain, arrays are lists of strings, each one distinguished from each other one. In this sense, bash is just like almost every other programming language.

This problem, and the solution I suggest as well as several other ones, is well-described in the Bash FAQ entry 50 (I'm trying to put a command in a variable, but the complex cases always fail!), which is worth reading in detail. (Link taken from a comment by @John1024.)


The proposed array is a good design approach, but curl still adds single quotes to the array items that already contain double quotes around them, thus making invalid headers.