Executing bash file with Error bin/bash: bad interpreter: No such file or directory

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.


Solution 1:

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.