Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

I'm sorry to ask this but I don't understand why I've got this error:

line 1: #!/bin/bash: No such file or directory

while running any piece of bash script like this:

#!/bin/bash
echo "pouet"

I've tried running it on a Fedora 19 and everything went well.

I'm on Debian 7, I've tried parsing the first line to search for \n\r but everything was clean (every script I've made so far seems to behave the same way).

My $PATH looks like this: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin. I've tried it with a normal user or root, same result. But the rest of the script seems to behave normally (echo is interpreted). And yes, /bin/bash exists and is executable.

Edit:

$ LC_ALL=C sed -n l < tesT.sh
\357\273\277#!/bin/bash$
echo "pouet"$
share|improve this question
    
What does LC_ALL=C sed -n l < your-script give? –  Stephane Chazelas Jan 18 at 8:11
    
it gives me somthing like this : LC_ALL=C sed -n l < tesT.sh \357\273\277#!/bin/bash$ echo "pouet"$ –  doctori Jan 18 at 8:15
    
please add the output of head -1 yourscript | od -c –  michas Jan 18 at 8:18
1  
@Stephane Chazelas: According to that question the error message is the same. Also the solution presented there is exactly the right one in this case. –  michas Jan 18 at 8:28
1  
@michas, I agree the questions are probably the same (though the error messages differ), but the accepted answer there, though giving the right approach to investigate the problem, is not giving the right explanation for the problem. Here, and there, it's not a CRLF issue. –  Stephane Chazelas Jan 18 at 9:04
show 2 more comments

marked as duplicate by michas, Anthon, rahmu, slm, Zelda Jan 18 at 15:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 7 down vote accepted

See #!/bin/bash - no such file or directory.

Something is wrong with the first line of your script.

Use

head -n 1 yourscript | LC_ALL=C od -tc

Or:

LC_ALL=C sed -n 'l;q' your-script

to find out, what is wrong.

If the output starts with 357 273 277 it is an UTF8 byte order mark. In vim you can use set nobomb to remove it.

Neither the kernel (which interprets the she-bang line), nor your system's sh (which is invoked for scripts that don't have a she-bang) recognise that BOM, so what you're seeing here is:

  1. The kernel returns ENOEXEC upon executing that script as it's not a recognised executable (it's not a native binary executable, and it's not a script as it doesn't start with #!).
  2. Your interactive shell, upon that ENOEXEC, calls sh on it.
  3. That sh reads and interprets the file. The first line is not a comment, the # is not preceded by a blank, so it does try to execute <BOM>#/bin/bash, and reports an error message.
share|improve this answer
    
just consolidating all those comments into a real answer. –  michas Jan 18 at 8:52
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.