6

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"$
doctori
  • 213
  • What does LC_ALL=C sed -n l < your-script give? – Stéphane Chazelas Jan 18 '14 at 08: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 '14 at 08:15
  • please add the output of head -1 yourscript | od -c – michas Jan 18 '14 at 08:18
  • OK I get it, it was the UTF_8 BOM that have been inserted (by VIM ??) the output of head -1 tesT.sh | od -c{ gives me this : 0000000 357 273 277 # ! / b i n / b a s h \n I've removed the BOM with this : awk '{if(NR==1)sub(/^\xef\xbb\xbf/,"");print}' tesT.sh > test.sh.nobom – doctori Jan 18 '14 at 08:20
  • 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 '14 at 08:28
  • @doctori, set nobomb in vim to remove that BOM. Something in your ~/.vimrc must set bomb, you should remove it as it's almost always recommended not to use a BOM for UTF8. – Stéphane Chazelas Jan 18 '14 at 08:40
  • 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. – Stéphane Chazelas Jan 18 '14 at 09:04

1 Answers1

15

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.
michas
  • 21,510
  • just consolidating all those comments into a real answer. – michas Jan 18 '14 at 08:52
  • there are three invisible characters at the beginning of the file called BOM. You can see them in any hex viewer. There are many easy ways how to remove them: https://unix.stackexchange.com/questions/381230/how-can-i-remove-the-bom-from-a-utf-8-file – xerostomus May 26 '18 at 08:16