Using curl in a bash script and getting curl: (3) Illegal characters found in URL

The $URL contains a \r (CR) at the end (0d). Remove it with

URL=${URL%$'\r'}

before using it with curl.

I had the same problem, but it was caused by me having created the script through Windows, which meant that the end of line had an unnecessary \r.

A simple dos to unix conversion fixed it.

dos2unix <scriptname>
ichak khoury

It could be an enter at the end of the file. Try to remove it or delete it, note that the enter character is not something view-able....