Script Interpreter

Script Interpreter#

If an executable file is run, the kernel checks the first bytes of the file to choose the right handler to continue. If the file begins with #! it is treated as a script file. This marker is known as shebang or hashbang. It is followed by the full path of the interpreter to be used for the rest of the file. The kernel runs interpreter and passes over the script.

This first line does no harm to the logic of script itself as lines beginning with # are treated as comments.

The interpreter is usually a real scripting language interpreter as /bin/bash or /bin/ptython but could be any program. It has to be specified by full path. The which command helps to quickly find the correct path, e.g. which bash gives /usr/bin/bash.

A simple shell script (say hello.sh) may look like this:

#!/usr/bin/bash
echo "Hello World"

It can be executed like any other program.

$ chmod u+x hello.sh
$ ./hello.sh
Hello world

The same could be realized as a python script, e.g. hello.py:

#!/usr/bin/python
print("Hello world")