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

Some times 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, like this:

vi -b myfile.sh

Opening the file in vi using 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 the magic vi command:

:1,$s/^M//g

Note that you don't really type a ^ character and then a M character to generate that command. You actually type a [Control][v] and then a [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

dos2unix filename works too...

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

Thanks

Hi,

Solved my problem immediately. Thanks for the solution.

bad interpreter error

magic vi command worked for me !!!

bad interpreter - thanks!

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

Thnx :)

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

THANK YOU!!

Thanks man,

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

Thanks a lot!

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.

OH LORD THANKS SO MUCH!!

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.

Legend

This saved me many hours of frustration - THANKS

Thanks for the tip - exactly

Thanks for the tip - exactly my problem as described

Thank you

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!

Post new comment

The content of this field is kept private and will not be shown publicly.