0

I have a bash script like this:

myvar="--silent"
myurl="http://duckduckgo.com"
curl "${myvar}" "${myurl}"

This works as expected, I get the response body. Now if I want to remove the "--silent" parameter, I do this:

myvar=""
myurl="http://duckduckgo.com"
curl "${myvar}" "${myurl}"

Now the requests again works as expected, but I get this error message before the response body:

curl: (3) \<url\> malformed

If I change the script like this, then it works fine without error messages:

curl ${myvar} "${myurl}"

I would like to understand why this is happening.

danisan00
  • 404
  • 4
  • 5
  • `bash -x /your/script.sh` – pynexj Jun 23 '23 at 13:39
  • 2
    `"${myvar}"` expands to empty string, which is not a valid url. you should use an array instead, like `myvar=(--silent)` and `curl "${myvar[@]}" "${myurl}"` – oguz ismail Jun 23 '23 at 13:40
  • 1
    If you want something that could expand to *nothing*, rather than an empty string (which is still *something*), you need to use an array. `myvar=()`; `curl "${myvar[@]}" "${myurl}"`. – chepner Jun 23 '23 at 17:18
  • An *unquoted* parameter expansion can expand to nothing (if the expansion produces an empty string), but an unquoted parameter expansion that contains something may not behave the way you want. (Consider `myvar=*`, for example.) – chepner Jun 23 '23 at 17:19
  • See ["How to (not) pass empty quoted variables as arguments to commands"](https://stackoverflow.com/questions/20306739/how-to-not-pass-empty-quoted-variables-as-arguments-to-commands/). – Gordon Davisson Jun 23 '23 at 18:56

0 Answers0