Ruby: Bad Interpreter

If it ever happened to you to see a message like below starting your shining Ruby script from the Unix shell, you probably know how disappointing it can be.

: bad interpreter: No such file or directory

Yesterday I wrote a highly useful helper tool on Ruby, but every time I tried to start it without explicitly mentioning a Ruby interpreter in the shell command, it gave me the above error. Truth be told, it looked confusing; it almost melted my brain. The script had the correct shebang! pointing exactly to the interpreter, and I could invoke it manually using that path, but for some mysterious reason it refused to work alone, like this:


After a grueling scanning of forums I managed to find a blurry explanation and a dirty trick to fix this quickly. The thing is, the trailing ^M codes in code lines make the shell through out the error. The only way to fix that is to remove all these codes. Doing so manually is a pain, I know, hence the little shell command that I would love to share with you today.

Assuming the script name is “script.rb”, it looks as follows:

cat script.rb | tr -d '\15\32' > script.rb

The code dumps the script file into a pipe to the ‘tr’ command that cuts out all ^M’s and routes the output back to the script file. Hope you’ll find it useful.