108

I've created a bash script but when I try to execute it, I get

#!/bin/bash no such file or directory

I need to run the command: bash script.sh for it to work.

How can I fix this?

  • I have this issue now under cygwin with a script I could swear was already running without problems. I checked all answers, but none seems to fit. Other questions and answers also mentioned 32/64 bit issues, but for shell scripts this could be excluded, right? – jan Jun 18 '18 at 08:18
  • Found the reason, added details in new anwer https://unix.stackexchange.com/a/450389/62636 in case somebody has also used #!/usr/bin/env bash instead of #!/bin/bash and also looking here... – jan Jun 18 '18 at 08:33

8 Answers8

152

This kind of message is usually due to a buggy shebang line, either an extra carriage return at the end of the first line or a BOM at the beginning of it.

Run:

$ head -1 yourscript | od -c

and see how it ends.

This is wrong:

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

This is wrong too:

0000000 357 273 277   #   !   /   b   i   n   /   b   a   s   h  \n

This is correct:

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

Use dos2unix (or sed, tr, awk, perl, python…) to fix your script if this is the issue.

Here is one that will remove both of a BOM and tailing CRs:

sed -i '1s/^.*#//;s/\r$//' brokenScript

Note that the shell you are using to run the script will slightly affect the error messages that are displayed.

Here are three scripts just showing their name (echo $0) and having the following respective shebang lines:

correctScript:

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

scriptWithBom:

0000000 357 273 277   #   !   /   b   i   n   /   b   a   s   h  \n

scriptWithCRLF:

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

Under bash, running them will show these messages:

$ ./correctScript
./correctScript
$ ./scriptWithCRLF
bash: ./scriptWithCRLF: /bin/bash^M: bad interpreter: No such file or directory
$ ./scriptWithBom
./scriptWithBom: line 1: #!/bin/bash: No such file or directory
./scriptWithBom

Running the buggy ones by explicitely calling the interpreter allows the CRLF script to run without any issue:

$ bash ./scriptWithCRLF
./scriptWithCRLF
$ bash ./scriptWithBom
./scriptWithBom: line 1: #!/bin/bash: No such file or directory
./scriptWithBom

Here is the behavior observed under ksh:

$ ./scriptWithCRLF
ksh: ./scriptWithCRLF: not found [No such file or directory]
$ ./scriptWithBom
./scriptWithBom[1]: #!/bin/bash: not found [No such file or directory]
./scriptWithBom

and under dash:

$ ./scriptWithCRLF
dash: 2: ./scriptWithCRLF: not found
$ ./scriptWithBom
./scriptWithBom: 1: ./scriptWithBom: #!/bin/bash: not found
./scriptWithBom
jlliagre
  • 61,204
  • 3
    Another way of revealing if this is the problem is hexdump -C yourscript | head -n 1. I would still use dos2unix yourscript to fix it. – Kevin M Dec 17 '11 at 23:32
  • Yes it could very well be that. I edited in on windows. Things for the tip. – Nicolas de Fontenay Dec 18 '11 at 02:17
  • 2
    If it were a CRLF problem, you wouldn't see a #!/bin/bash no such file or directory error message, as there's no reason anything would try to execute or open #!/bin/bash. It's /bin/bash<CR> what would be executed. – Stéphane Chazelas Jan 18 '14 at 08:47
  • 1
    @StephaneChazelas As dos2unix fixed the problem, there is little doubt it wasn't a CRLF problem. The error message was probably just inaccurately transcripted.. – jlliagre Jan 18 '14 at 18:28
  • 6
    dos2unix also removes an UTF-8 BOM. A UTF-8 BOM could have explained the error message. – Stéphane Chazelas Jan 18 '14 at 19:49
  • @StephaneChazelas I agree the error message would have included the shebang but the OP complains that the script was not executed when called as is, but correctly run when passed as a bash argument. With an UTF-8 BOM, the script would have been executed in both cases, not just the second one, and the error message would have been displayed in both cases too, which isn't what the OP is describing. – jlliagre Jan 18 '14 at 20:16
  • 1
    It is sad how incompatible Microsoft is. Even with something as basic as ASCII. – ctrl-alt-delor Jun 18 '18 at 09:02
  • @ctrl-alt-delor Yes. Note that Mac OS used to be ending its lines with a third alternative but is now aligned with Unix/Linux. See https://unix.stackexchange.com/questions/411811/why-does-linux-use-lf-as-the-newline-character/411830#411830 – jlliagre Jun 18 '18 at 09:06
  • @jlliagre I was commenting about adding a BOM, to the start of the file (it is not even part of UTF-8, and they add it to the start of an ASCII file as well). However you are correct, they do line endings differently as well, making their text files 2%→5% bigger, and incompatible. – ctrl-alt-delor Jun 18 '18 at 09:50
  • @jlliagre On current macOS a BOM will screw everything up. – Clearer Oct 29 '18 at 13:15
  • 1
    @Clearer On older too. AFAIK a BOM screws everything up whatever the OS. – jlliagre Oct 31 '18 at 09:46
22

This can also be caused by a BOM in a UTF-8 script. If you create the script in Windows sometimes you get some junk at the start of the file.

user253751
  • 2,266
teknopaul
  • 703
13

Actualy, the right shebang for bash script is this:

#!/usr/bin/env bash

Because, in freeBSD, bash is located in /usr/local/bin/bash

  • 14
    "right" is a difficult word to use in such cases. Perhaps a better phrase would be "less error-prone". – HalosGhost Jun 27 '14 at 20:56
  • 3
    This is terrible too; the assumption that /usr exists is a bad one IMO. Haiku, for instance, does not have /usr. – jessicah Jul 05 '16 at 21:48
  • @jessicah: /usr (as well as /usr/bin/env) are mandatory paths in a POSIX system, be it Linux, FreeBSD or even MacOS. Haiku is not a *nix system, is a cloud computing platform, so you cannot assume any path or tool. – MestreLion May 14 '22 at 09:42
  • Great... This worked for me in CentOS 7 – Farrukh Waheed Jan 12 '23 at 21:38
11

You can use vi to fix both problems if they exist:

vi <your_file>
:set ff=unix
:set nobomb
:wq
cwash
  • 219
  • Answers should be self-contained as much as possible.  The question doesn’t mention two problems; if you’re going to build on other answers, you should at least say what they are.  Better yet, you should explain how this answers the question. – G-Man Says 'Reinstate Monica' May 31 '17 at 20:42
  • Very quick fix without downloading more windows tools, thanks! – steampowered Aug 16 '17 at 21:00
  • 2
    @G-Man Other answers already mention this in much better detail than I wish to go into. No need to repeat, but if it's not painfully obvious, you can have a WIndows line ending and a hidden Windows BOM character. I think a lot of people that are scanning answers appreciate brevity instead of being self contained, especially when there is much more detail in other answers. – cwash Aug 18 '17 at 00:27
4

If you don't have dos2unix this is a way to fix this issue.

cp script _p4 && tr -d '\r' < _p4 > script && rm _p4
cokedude
  • 1,121
4

Byte-order Mark (BOM)

This could be caused by a BOM. From Wikipedia, a BOM is a

The byte order mark (BOM) is a Unicode character, U+FEFF byte order mark (BOM), whose appearance as a magic number at the start of a text stream can signal several things to a program consuming the text

Unfortunately, it doesn't signal anything to the Linux kernel that handles the she-bang line. You can verify you have a BOM by using file,

file /tmp/foo 
/tmp/foo: UTF-8 Unicode (with BOM) text

Or you can hexdump the first few characters and see if they match any of the BOM characters manually

You can strip the BOM characters once you know them like this,

sed -i '1 s/^\xef\xbb\xbf//' *.txt
GAD3R
  • 66,769
Fabien Haddadi
  • 819
  • 1
  • 6
  • 10
  • Works great! And I can confirm that you *only need to remove the BOM once. After I removed it, I could continue to edit the file in Visual Studio and the BOM did not reappear. Thanks! – John Henckel Nov 21 '22 at 22:01
0

I had the issue by accidentally adding a wrong bash executable to the PATH and because in my script the more flexible #!/usr/bin/env bash shebang was used (take first bash executable from path).

command -v bash
/cygdrive/c/Program Files/Git/bin//bash

I have installed GIT for Windows to work in cygwin together with Windows GIT GUIs (was not working with cygwin native git...). I solved this now by switching to #!/bin/bash sheband and removing GIT for windows from PATH.

jan
  • 105
-3

Try #!/bin/bash

Second thing: find / -name bash
Third thing: ls -al /bin/bash

nopcorn
  • 9,559
  • 1
    Or just which bash. We know it's finding one because it's working with bash script.sh. – Kevin Dec 18 '11 at 02:58
  • True. And as mentioned, there is the much more portable /usr/bin/env method to have a program locate bash (or an other interpreter) for you. No need to hardcode a pah. – Hennes Jul 17 '14 at 17:44