How can I configure bash to handle CRLF shell scripts?

mcandre picture mcandre · Jan 30, 2013 · Viewed 9.1k times · Source

I want to execute bash scripts that happen to use Windows/CRLF line endings.

I know of the tofrodos package, and how to fromdos files, but if possible, I'd like to run them without any modification.

Is there an environment variable that will force bash to handle CRLF?

Answer

Magnus Gustavsson picture Magnus Gustavsson · Jan 30, 2013

Perhaps like this?

dos2unix < script.sh|bash -s

EDIT: As pointed out in the comments this is the better option, since it allows the script to read from stdin by running dos2unix and not bash in a subshell:

bash <(dos2unix < script.sh)