From: Jeremy Bopp Date: 2011-03-03T13:43:35+09:00 Subject: Re: some unrecognized syntax error On 03/02/2011 06:34 PM, Eric Christopherson wrote: > I've noticed recently that I can run Ruby files with DOS or Unix line > endings with no problem on either platform, but as soon as I put a > shebang line in the file, and it has DOS line endings, I can't run it > directly from the command line on OS X (e.g. `./test.rb`). I have to > run it specifically with 'ruby' instead (e.g. `ruby ./test.rb`). Is > that just a built-in problem with OS X or bash's reading of shebang > lines? You didn't mention exactly what error you received when trying to run the file with DOS line endings but it was probably something like this: bash: ./test.rb: /bin/ruby^M: bad interpreter: No such file or directory The problem is actually within the lower levels of the operating system, below bash and ruby. When a non-binary file is executed, the part of the kernel that handles running programs looks to see if there is a shebang line. If there is, it reads until it encounters a LF (newline) character. Everything between the shebang and the LF is the command line to actually run, to which the path to the file itself will be appended. In the case above, the CR (carriage return) part of the DOS line ending is being consumed as part of the command line. Obviously, there is no program named /bin/ruby^M. Note that the ^M is actually bash being helpful by converting the CR character into something readable. When that isn't done, you'll likely see the following on your terminal: : No such file or directory If you redirect stderr to a file or pipe it to less, you'll actually see something like this: /usr/bin/env: ruby^M: No such file or directory Which is pretty much the same problem as the first example, just harder to diagnose. The moral of the story is to avoid DOS line endings in your scripts if you expect to run them directly. There are probably many other ways to hack around the issue, but simply using Unix line endings is going to be among the easiest. You can read up more about shebang on Wikipedia. Pay particular attention to the portability section: http://en.wikipedia.org/wiki/Shebang_%28Unix%29 -Jeremy