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

2 Answers 2

6

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.

1
  • 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, 2020 at 7:37
-1

Add on to this . I have been facing this problem on mac terminal. This got resolve after leaving the first line and start from the second line in the script. It worked for me. Solution

2
  • With that approach, you can as well just remove #!bin/bash because this now just is a comment. The approach will fail for scripts not executable by bash though.
    – nohillside
    Apr 16 at 15:39
  • Thanks !! Actually I didnt switch to bash from zsh in mac. ZSH is a default shell. Changed to bash using chsh -s /bin/bash . now able to execute without leaving the first line. #!/bin/bash echo "Hello world!! Apr 17 at 15:30

You must log in to answer this question.

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