12

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?

mcandre
  • 22,868
  • 20
  • 88
  • 147
  • 1
    No, there is not such an environmental variable. – fpmurphy Jan 30 '13 at 17:52
  • 1
    I almost left a bogus answer - with bash on cygwin you can do `set -o igncr`, and there was talk of extending that to bash on Linux, but it never happened apparently. – William Jan 30 '13 at 19:24

2 Answers2

12

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)
  • 2
    This will fail if the script requires any kind of input. In those cases, you can use `bash <(dos2unix yourscript)` – that other guy Jan 30 '13 at 18:24
  • 2
    I think you mean `bash <(dos2unix < yourscript)`. Otherwise it will convert the file and write nothing to stdout. But, yes, that's a better option. – Magnus Gustavsson Jan 30 '13 at 19:23
8

Here's a transparent workaround for you:

cat > $'/bin/bash\r' << "EOF"
#!/bin/bash
script=$1
shift
exec bash <(tr -d '\r' < "$script") "$@"
EOF

This gets rid of the problem once and for all by allowing you to execute all your system's Windows CRLF scripts as if they used UNIX eol (with ./yourscript), rather than having to specify it for each particular invocation. (beware though: bash yourscript or source yourscript will still fail).

It works because DOS style files, from a UNIX point of view, specify the interpretter as "/bin/bash^M". We override that file to strip the carriage returns from the script and run actual bash on the result.

You can do the same for different interpretters like /bin/sh if you want.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • 2
    They do different things. dos2unix modifies scripts, while this allows you to run them unmodified (as per original question). – that other guy Jan 30 '13 at 18:00
  • And why would you want to modify the script each time you run it instead of modifying it once and be done? Is CPU time for free? – m0skit0 Jan 31 '13 at 08:45
  • 1
    Without argument `dos2unix` reads from stdin and writes to stdout (is a filter) so it doesn't necessarily modify the file. – Magnus Gustavsson Jan 31 '13 at 11:56
  • You either have to modify the file or modify the command used to run it. This approach doesn't require you to modify anything. – that other guy Jan 31 '13 at 17:07
  • No. Again, `dos2unix` doesn't require you to modify anything (unlike `fromdos`). – Magnus Gustavsson Feb 07 '13 at 07:52
  • Ok, then explain how I can run my unmodified `script.sh` with my unmodified command `./script.sh` – that other guy Feb 07 '13 at 17:05
  • @m0skit0 You would have to modify every time you check out from Git, and that gets annoying. – Steve Pitchers Dec 07 '15 at 16:01
  • @StevePitchers You're a programmer. Automate it. – m0skit0 Dec 07 '15 at 16:27
  • 1
    This solution does not work for sourcing crlf formatted files – Henning Mar 23 '16 at 16:08
  • re: `why modify the script each time, instead of just once` because 99% of the time, the CRLF is just fine; 90% of the time I am in cygwin with a bash that understands CRLF, 9% of the time I am in a real Windows CMD window and needs the CRLF, and only 1% of the time am I in a real LINUX bash that doesn't understand CRLF. – Jesse Chisholm Apr 18 '17 at 00:34