xargs: how to have literal double-quotes in replacement?
bash, curl, json, xargs
Solution
GNU Parallel was built specifically to deal with `xargs` bad handling of special chars:
<source parallel curl -X POST -H "application/json" myURL -d {}
Not only will it quote " correctly, it will quote any string correctly, so it will be interpreted as a single argument by curl.
Added bonus: Your queries will run in parallel - one query per cpu.
Problem
I have a source file with JSON-Objects one per line like this: source: ``` {"_id":"1","name":"one"} {"_id":"2","name":"two"} {"_id":"3","name":"three"} ``` I want to send each line to a ``` curl -X POST -H "application/json" myURL -d '<REPLACEMENT>' ``` The double quotes do not make it to curl when I am trying ``` <source xargs -I % curl -X POST -H "application/json" myURL -d '%' ``` I tried escaping the quotes in the curl command and later I replaced all double-quotes in the source file with \". I found no version to work. Another approach to use seq with sed to write each line into a temp file and curl -d @temp did not work out for me. Is there an elegant solution or do I have to write a script with a loop?