Shell script error - bad interpreter - No such file or directory

Unix/Linux FAQ: How do I solve the problem of the “bad interpreter” error message?

Sometimes when you take a file from a DOS/Windows system and move it to a Linux or Unix system you’ll have problems with the dreaded ^M character. This happened recently when I moved an Ant script from a Windows system to my Mac OS X system. When I tried to run the shell script under the Mac Terminal I got this “bad interpreter” error message:

: bad interpreter: No such file or directory

I couldn’t figure out what the message meant at first, but finally it hit me: the dreaded ^M problem. Sure enough, I opened the file in the vim editor with the -b (binary), like this:

vi -b

Opening the file in vi/vim with the “binary” switch (-b), I saw the extra ^M at the end of each line. I then issued one of my favorite vi commands to remove all these ^M characters, and the script magically began working. Here’s that magic vi command:


Note that you don’t really type a ^ character and then a M character to generate that command. You actually type [Control][v] and then [Control][m] to create the necessary character. I don’t know the genesis of this, but if you'll type those two keystrokes I think you’ll see that it works as described.



dos2unix filename works too...

Thanks for this. I just spent a good ten minutes trying to figure out what was wrong with my script.



Solved my problem immediately. Thanks for the solution.


Man, you took a big frustration out of me, this was driving me crazy. Your simple solution worked nicely and smoothly, thanks a lot!


Solved my problm too...was wondering what could be wrong since morng ! :)


Thanks man,

You are a true good samaritan, citizen of the world person. Thank you o so very much sir.


No more complex commands or applications to get rid of this issue.
Of course, I can use native *NIX boxes instead but networks are getting more complex every day...
Thanks again for sharing this.


Man that hit the spot let me tell you, thanks so much and especially using the exact error message as your title, found it right at the top of the results. Now if I would have just google'd it like 3 hours ago instead of questioning every single line in my script(a 396 line shell script) i would have gotten to have lunch today. lol oh well live and learn, thanks again for sharing you rock.


This saved me many hours of frustration - THANKS


Being new to shell, this totally caught me off guard. You were the first Google result for the bug and it solved the problem immediately. Thanks!