To start off, I need to be clear about a few things:
- Based on the tags to the question, I see we are in PowerShell rather than a linux/unix or even Windows cmd shell
- In spite of this, we are using Unix
curl
(probably curl.exe
), and not the PowerShell alias for Invoke-WebRequest
. We know this because of the -sL
argument. If Powershell was using the alias, we'd see a completely different error.
Next, I need to talk briefly about line endings. Instead of just a single LF (\n
) character as seen in Unix/linux and expected by bash, Windows by default uses the two-character LF/CR pair (\n\r
) for line endings.
With all that background out of the way, I can now explain what's causing the problem. It's this single pipe character:
|
This a PowerShell pipe, not a Unix pipe, so the operation puts the output of the curl
program in the PowerShell pipeline in order to send it to the bash
interpreter. Each line is an individual item on the pipeline, and as such no longer includes any original line breaks. PowerShell pipeline will "correct" this before calling bash using the default line ending for the system, which in this case is the LF/CR pair used by Windows. Now when bash tries to interpret the input, it sees an extra \r
character after every line and doesn't know what to do with it.
The trick is most of what we might do in Powershell to strip out those extra characters is still gonna get sent through another pipe after we're done. I guess we could tell curl to write the file to disk without ever using a pipe, and then tell bash to run the saved file, but that's awkward, extra work, and much slower.
But we can do a little better. PowerShell by default treats each line returned by curl as a separate item on the pipeline. We can "trick" it to instead putting one big item on the pipeline using the -join
operation. That will give us one big string that can go on the pipeline as a single element. It will still end up with an extra \r
character, but by the time bash sees it the script will have done it's work.
Code to make this work is found in the other answer, and they deserve all the credit for the solution. The purpose of my post is to do a little better job explaining what's going on: why we have a problem, and why the solution works, since I had to read through that answer a couple times to really get it.