Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a weird situation where some of my shell scripts that use #!/bin/sh as the shebang sometimes work - other times they yield the following message:

bash: ./ /bin/sh^M: bad interpreter: No such file or directory

Is there something simple that I'm missing?

share|improve this question
have you check your sh file if there is really a ^M behind the shebang? – lamwaiman1988 May 20 '11 at 9:12
there definitely isn't – Kevin Duke May 20 '11 at 9:20
but why the bash state there is a ^M? try using another editor to view the script file. – lamwaiman1988 May 20 '11 at 9:26
Did you made this file in windows and ftp to unix? – lamwaiman1988 May 20 '11 at 9:28
njd's answer explains quite nicely ;) – Kevin Duke May 20 '11 at 9:28
up vote 1 down vote accepted

That ^M is a dead giveaway - it looks like the script has Mac line-endings (Ctrl-M) instead of Unix line-endings (Ctrl-J).

It won't appear as a literal ^M - in fact depending on your text editor it might not be shown at all.

You don't say which operating system you have, so I'm not sure which tools you have on your system but could you try to get a character (not hex) dump with:

od -tc

...and tell us whether the /bin/sh is followed by \r, \n or \r\n ?

I'm expecting you'll see something like:

0000000   #   !   /   b   i   n   /   s   h  \r

Or if you run:

cat -e

...that will display the carriage return character if it's there.

share|improve this answer
it looks like \r \n. I wrote this code in notepad++ on windows, and executed it in ssh on linux. – Kevin Duke May 20 '11 at 9:26
That's probably the problem, I would have never guessed! Anyhow, any tips to fix this? +1 good answer :) – Kevin Duke May 20 '11 at 9:27
` cat -e #!/bin/sh^M$ ` – Kevin Duke May 20 '11 at 9:30
Just copy the content from the windows, and in the session of linux, create a new file using vim and paste the content in it. – lamwaiman1988 May 20 '11 at 9:30
In Notepad++, Edit > EOL Conversion > UNIX Format; or use 'dos2unix' on the Linux end. – njd May 20 '11 at 9:36

dos2unix will fix it.

Edit: Alternatively, if for some reason you aren't able to find dos2unix, this simple script will do it:

tr -d '\r' < > 
share|improve this answer
unknown command. I can't find the package via apt-get either – Kevin Duke May 20 '11 at 9:31
It would help if you tell what Operating System you are using. – jlliagre May 20 '11 at 9:39
Debian Lenny. . – Kevin Duke May 20 '11 at 9:40
apt-get install tofrodos – jlliagre May 20 '11 at 9:44
Recent Ubuntu/Debian distros don't have it; you'll need to run: apt-get install tofrodos first, and use fromdos instead. – njd May 20 '11 at 9:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .