why do curl commands generate different results when running in terminal?

Using exec is not a shell – you can’t and don’t have to quote for a shell, that is not there. Further exec(String) uses by default a string tokenizer (which basically splits at whitespace) to make it particularly useless for any slightly advanced usecase.

You are most likely always better off to use the version that accepts a string array for the command (+ args).

What you where effectively calling looked like this (note, that the command gets split at whitespace — so I used \' to make my shell ignore that):

# curl -LH \'Accept: application/x-bibtex\' http://dx.doi.org/10.1016/j.immuni.2015.09.001
curl: (6) Could not resolve host: application
... HTML ...

The shortest route using groovy looks like this (note that exec also has a version for passing in an array of strings):

groovy:000> ["curl", "-LH", "Accept: application/x-bibtex", "http://dx.doi.org/10.1016/j.immuni.2015.09.001"].execute().text
===> @article{Biswas_2015,
9doi = {10.1016/j.immuni.2015.09.001},
9url = {https://doi.org/10.1016%2Fj.immuni.2015.09.001},
9year = 2015,
9month = {sep},
9publisher = {Elsevier {BV}},
9volume = {43},
9number = {3},
9pages = {435--449},
9author = {Subhra~K. Biswas},
9title = {Metabolic Reprogramming of Immune Cells in Cancer Progression},
9journal = {Immunity}
}

If you need “shell-isms”, then use ["sh", "-c", command] instead.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top