5

I've been looking for a solution to my problem all the morning, especially in the 4 posts in https://stackoverflow.com having the same error name in their title but the solutions don't work for me.

I want to do several simple cURL requests put together in a Bash script. The request at the end of the file always works, whatever request it is. However the requests before return an error:

curl: (3) Illegal characters found in URL

I am pretty sure that it has something to do with the carriage return in my file. But I don't know how to deal with it. As I show in the picture below I tried to use ${url1%?}. I also tried ${url1%$'\r'}, but it doesn't change anything.

Screenshot of file + results in terminal:

screenshot of file + results in terminal

Any ideas?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Héloïse Chauvel
  • 502
  • 3
  • 6
  • 21

1 Answers1

8

If your lines end with \r, stripping away the \r from the $url won't work, because the line

curl -o NUL "{url1%?}

also ends with a \r, which is appended to the url argument again.

Comment out the \r, that is

url1="www.domain.tld/file"
curl -o NUL "${url1%?}" #

or

url1="www.domain.tld/file" #
curl -o NUL "$url1" #

or convert the file before executing it

tr -d '\r' < test.sh > testWithoutR.sh
Socowi
  • 25,550
  • 3
  • 32
  • 54
  • The comment doesn't work (the terminal still detects it) but your last trick does, thanks. Do you know why the file put automatically a `\r` at the end of each line? – Héloïse Chauvel Apr 25 '17 at 12:22
  • 1
    Adding null comments to the end of every line is a terrible suggestion, compared to simply saving the file with the expected newline characters in the first place. – chepner Apr 25 '17 at 12:25
  • Thank you chepner for putting in relation the other post with mine. I have changed the line endings format and now it works. – Héloïse Chauvel Apr 25 '17 at 12:40