1

I am very new to bash and try writing the first script named hello_world in the path of /Users/me/Study/Linux with the content written by vim:

#! bin/bash
echo Hello World

However, the error occurs when executed:

-bash: /Users/me/Study/Linux/hello_world: bin/bash: bad interpreter: No such file or directory

I have read many questions on the issue but still feel totally lost. :( I'll be very thankful for your help.

  • 3
    try #! /bin/bash for the first line – Ted Wrigley Apr 5 at 3:44
  • @TedWrigley Thx, that works; but why? – Chris Tang Apr 5 at 4:12
  • 1
    The shebang (#!) takes a path to an executable, and in Unix/linux, paths have to begin with a slash. – Ted Wrigley Apr 5 at 6:35
5

The first line of your bash script must look like this.

#!/bin/bash

This is called a "shebang line" because the first two characters #! are a shebang. The shebang line tells the computer which executable program should be used to interpret the rest of the script. In this case you're writing a bash script, but you could just as easily be writing a script for python or awk, and you need to tell the system which interpreter to use based on what language the script is written in.

The reason that your shebang line #! bin/bash doesn't work is because you haven't provided a valid path (typically, an absolute path) to the interpreter. If you omit the initial slash / then the shell looks for bin in the current working directory but doesn't find it.

Since you're just getting started learning bash, I suggest reading a book. This will save you from having to ask too many questions. Learning the bash Shell, 3rd Edition by Cameron Newham is quite good.

| improve this answer | |
  • 4
    Note that the space after the #! is unimportant - #!/bin/bash and #! /bin/bash both work and #!bin/bash or #! bin/bash will only work if you cd / first (so bin is found). From your Wikipedia link "Blanks after ! are OK" – lx07 Apr 5 at 7:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .