12

bad interpreter: No such file or directory

96px-Shebang

Earlier today I was trying to run a Python script that I downloaded from the internet. Bash (or the kernel, to be exact) refused to run the script, insisting that python didn’t exist:

$ mem.py
-bash: /home/natan/bin/mem.py: /usr/local/bin/python2.5^M: bad interpreter: No such file or directory

The script agreed to run indirectly with python ~/bin/mem.py
I googled and discovered that the file was encoded in DOS format with improper line endings.

To fix the problem, I copied the contents of the file and pasted them into a new file. Voilà. The shebang became magical again.

Then again, you can also fix the problem permanently. (Don’t.)

ln -s sh "sh$(printf "\r")"

photo credit: Sven & Lirion

  1. Zack says:

    It seems to me that it would be appropriate to patch the kernel to treat \r\n as end-of-line when processing #! lines.

  2. Dave says:

    How about the dos2unix command or using sed?

  3. cartman says:

    I would just go with a simple fix: dos2unix mem.py

  4. Simon says:

    Yeah, that’s a classic problem… can’t find interpreter^M. Ultimately, the only good answer is to not combine Unix-style shebang lines with Windows-style line formatting (and avoid Windows formatting in general).

  5. Roshan says:

    I encountered a similar problem today (the text was full of DOS line endings), and I found the following solution useful: http://vim.wikia.com/wiki/File_format#Converting_the_current_file

  6. Eric says:

    The ^M at the end on your “/bin/python2.5^M:” error message indicates this is due to messed up line endings, as Roshan discovered. It looks like you are trying to execute a file created with DOS line endings in a UNIX environment.

  7. Ttroels says:

    Thanks!

    You saved hours of work there….