"set -eo pipefail" not working in Windows Subsystem for Linux (Ubuntu 16.04) [duplicate]

I have installed Windows subsystem for Linux on my Windows 10 machine which is supposed to be emulating Ubuntu 16.04. When I try to execute a script that has the following 2 lines at the top of the script, it gives me the error :invalid option name set: pipefail

Script lines:

#!/bin/bash
set -eo pipefail

This script runs fine on my Mac and on a CentOS system I have. I have checked (and verified) my Ubuntu shell to ensure it supports the pipefail option in bash by running set -o.

The output of cat -net /path/to/myscript is:

#!/bin/bash^M$
set -eo pipefail^M$

This question was marked as a duplicate of "How to change Windows line-ending to Unix version" but I do not feel it is duplicative of that because I personally did not know that the problem I was having (that is outlined in my question) was being caused by the type of line-ending in my shell script. The answer in each of the questions is the same but 2 different questions were asked.


As indicated by the ^M sequences in your cat -net output, you have saved the script with Windows-style (CRLF) line endings rather than Unix-style (LF only)

In fact, we could have guessed that from the truncated error message: the "real" error is that pipefail\r is an invalid option name. However, the carriage return causes the cursor to reset to the start of the line and overwrite the preceding characters. Compare

$ cat -net bad.sh
     1  #!/bin/bash$
     2  set -eo fhqwhgads$

$ ./bad.sh
./bad.sh: line 2: set: fhqwhgads: invalid option name

and

$ cat -net bad.sh
     1  #!/bin/bash$
     2  set -eo pipefail^M$

$ ./bad.sh
: invalid option name: pipefail