Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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'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?

share|improve this question
up vote 40 down vote accepted

Usually, this kind of message is due to an extra carriage return at the end of the first line. 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 correct:

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

Use dos2unix to fix your script if this is the issue.

share|improve this answer
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 2:17
It's totally that :/ Thanks a lot! – Nicolas de Fontenay Dec 19 '11 at 0:12
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 8:47
@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

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.

share|improve this answer
BOM can be removed easily using awk, as in stackoverflow.com/questions/1068650/… – pauxu Oct 8 '14 at 13:49

Actualy, the right shebang for bash script is this:

#!/usr/bin/env bash

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

share|improve this answer
"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
There was heated debate at one point years ago on askubuntu.com about that. It was settled that it was the BASH script writer's responsibility to use the environment; but, this also means that in a very secure environment, you risk not knowing which BASH will be used - the system version or a malware version. – Christopher Jul 17 '14 at 16:32
This is terrible too; the assumption that /usr exists is a bad one IMO. Haiku, for instance, does not have /usr. – jessicah Jul 5 at 21:48

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

cp script _p4 && tr -d '\r' < _p4 > script && rm _p4
share|improve this answer

It is too basic to ask about it, but does the script have execution right? Can be added by chmod +x script.sh. And do You run it from the directory containing the script by ./script.sh?

share|improve this answer
I can not speak for all distributions, but when I do a create a test script with: #!/usr/bin/env bash echo Script is running! (no execute bit set) and I try to execute it with ./script.demo I get -bash: ./script.demo: Permission denied. chmod +x script.demo ./script.demo produces the expected "Script is running!" – Hennes Jul 17 '14 at 17:42

Try #!/bin/bash

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

share|improve this answer
Or just which bash. We know it's finding one because it's working with bash script.sh. – Kevin Dec 18 '11 at 2: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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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