5

what does #!/bin/bash at the 1st line mean ? Is it just a comment and ignored by the shell like all other comments in program

OR

Only this line is interpreted by the interpreter and all the other lines starting from # are ignored except #!/bin/bash.

terdon
  • 242,166

3 Answers3

11

The first Line tells the computer which interpreter to use while executing the file

Let's say you write a script using python, and while running this script you will use the python interpreter and how would computer know which interpreter to use, it will know through this line which is also called the Shebang, for python

#!/usr/bin/env python
print "Hello world"

let's say again you write a script in bash and you use the shebang for bash script which tell the computer to use the bash interpreter while executing this code

 #!/usr/bin/bash 
  echo "Hello world" 
psycho3
  • 277
0

That first line starting with #! is (on Linux) interpreted by the Linux kernel inside the implementation of execve(2) system call (which is invoked, after fork, by your shell for most commands - those that are not functions and not shell builtins).

You could mention the absolute path of other executables (such as /usr/bin/env, see env(1)). As a silly example, try making a script containing the only line #!/bin/cat /proc/self/maps then make that script readable and executable (with chmod a+rx) then run it.

The POSIX execve specification does not mention any shebang specific processing, but all the Unix-like systems I worked on (Linux, SunOS, HPUX, AIX, ...) did process it specifically in execve

The bash interpreter is skipping that line, as any other line starting with a #, as a comment.

Look also at binfmt_misc (Linux specific).

-2

Forces the script to run using the bash shell, rather than the current shell being used.

steve
  • 21,892