Tell me more ×
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

2 Answers

up vote 9 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

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

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.