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?
bashexecutableshebangshell-script
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?
Best Answer
This kind of message is usually due to a bogus shebang line, either an extra carriage return at the end of the first line or a BOM at the beginning of it.
Run:
and see how it ends.
This is wrong:
This is wrong too:
This is correct:
Use
dos2unix
(orsed
,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:
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:
scriptWithBom:
scriptWithCRLF:
Under bash, running them will show these messages:
Running the bogus ones by explicitely calling the interpreter allows the CRLF script to run without any issue:
Here is the behavior observed under
ksh
:and under
dash
: