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

2 Answers

up vote 12 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. –  Stephane 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

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