Handling of end-of-line characters in the Bash shell has changed from the version of Cygwin shipped with pre-11.2 PGI releases. Here are some notes about Bash and line endings from the cygwin-announce email list. Note that this is Cygwin-generated documentation.
When using binary mounts, cygwin programs try to emulate Linux. Bash on Linux does not understand \r\n line endings, but interprets the \r literally, which leads to syntax errors or odd variable assignments. Therefore, you will get the same behavior on Cygwin binary mounts by default.
d2u is your friend. You can use it to convert any problematic script into binary line endings.
Cygwin text mounts automatically work with either line ending style, because the \r is stripped before bash reads the file. If you absolutely must use files with \r\n line endings, consider mounting the directory where those files live as a text mount. However, text mounts are not as well tested or supported on the cygwin mailing list, so you may encounter other problems with other cygwin tools in those directories.
This version of bash has a cygwin-specific set option, named “igncr”, to force bash to ignore \r, independently of cygwin’s mount style. As of bash-3.2.3-5, it controls regular scripts, command substitution, and sourced files. I hope to convince the upstream bash maintainer to accept this patch into a future bash release even on Linux, rather than keeping it a cygwin-specific patch, but only time will tell. There are several ways to activate this option:
4a. For a single affected script, add this line just after the she-bang:
(set -o igncr) 2>/dev/null && set -o igncr; # comment is needed
4b. For a single script, invoke bash explicitly with the option, as in
bash -o igncr ./myscript' rather than the simpler './myscript'
4c. To affect all scripts, export the environment variable BASH_ENV, pointing to a file that sets the shell option as desired. Bash will source this file on startup for every script.
4d. Added in the bash-3.2-2 release: export the environment variable SHELLOPTS with igncr included in it. It is read-only from within bash, but you can set it before invoking bash; once in bash, it auto-tracks the current state of ‘set -o igncr’. If xported, then all bash child processes inherit the same option settings; with the exception added in 3.2.9-11 that certain interactive options are not inherited in
4e. bash-4.1.9-1 dropped support for ‘shopt -s igncr’; it did not make sense to support the option through both set and shopt, and SHELLOPTS proved to be more powerful.
You can also experiment with the IFS variable for controlling how bash will treat \r during variable expansion.
There are varying levels of speed at which bash operates. The fastest is on a binary mount with igncr disabled (the default behavior). Next would be text mounts with igncr disabled and no \r in the underlying file. Next would be binary mounts with igncr enabled. And the slowest that bash will operate is on text mounts with igncr enabled.