#!/bin/bash – no such file or directory

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?

Try #!/bin/bash

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

Answered By: Sebastian Szary

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.


$ 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:


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


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


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

Under bash, running them will show these messages:

$ ./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

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

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

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]

and under dash:

$ ./scriptWithCRLF
dash: 2: ./scriptWithCRLF: not found
$ ./scriptWithBom
./scriptWithBom: 1: ./scriptWithBom: #!/bin/bash: not found
Answered By: jlliagre

Actualy, the right shebang for bash script is this:

#!/usr/bin/env bash

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

Answered By: moebius_eye

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.

Answered By: teknopaul

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

cp script _p4 && tr -d 'r' < _p4 > script && rm _p4
Answered By: cokedude

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/^xefxbbxbf//' *.txt
Answered By: Fabien Haddadi

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

vi <your_file>
:set ff=unix
:set nobomb
Answered By: cwash

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.

Answered By: jan