Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
    
What are the permissions of your script? Also, it should be bin/bash not binbash. –  Faheem Mitha Dec 17 '11 at 15:28
    
sorry typo. I will edit it. –  ndefontenay Dec 17 '11 at 15:36
add comment

5 Answers 5

up vote 16 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. –  ndefontenay Dec 18 '11 at 2:17
    
It's totally that :/ Thanks a lot! –  ndefontenay 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 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 at 18:28
show 2 more comments

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 at 17:42
add comment

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

share|improve this answer
add comment

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
2  
"right" is a difficult word to use in such cases. Perhaps a better phrase would be "less error-prone". –  HalosGhost Jun 27 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 at 16:32
add comment

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 at 17:44
add comment

Your Answer

 
discard

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.