Bash - runs scripts seamlessly with end-of-line CRLF

advertisements

I am using VM (in my case simply boot2docker) to run docker containers on Windows host. For convinience, my source files are mapped from host file system, so text files are by default using Windows-style CRLF line endings instead of Unix-style LF endings.

When I try to run some .sh file from docker container, I'll get an error

bash: ./script.sh: /bin/bash^M: bad interpreter: No such file or directory

Is there a way how could I somehow tell bash/sh interpreter to automatically convert \r\n to \n and run a file?

Sure, I could do some piplelining like this cat script.sh | tr -d "\r" | sh or even create an alias for that, but it would not cover situation where one script includes another.

The only acceptable solution I have found so far, is to set Git to checkout source files in UNIX format.


For some scripts the solution ln -s /bin/bash /bin/bash^M will fail.
So create a script /bin/bash^M that will dos2unix the arguments they get and run them with "[email protected]".

Edit: I tried it with the following bash^M script:

#!/bin/bash
PROG=$1
shift
dos2unix $PROG > /dev/null 2>&1
$PROG "[email protected]"

This will fail when your script includes other scripts with a dot, in that case you should avoid this work-around and go for the Unix format.
You said you can checkout in Unix format, or you can self change the files.
And commit into git, it is an improvement to have unix files stored in git in unix format. When you really want to edit them under windows, change the settings of your editor or dos2unix afterwards.