When we run the script as python script.py
, we actually python script.py
interpreter, which is usually located in /usr/bin/python
(the output of which python
will tell you exactly).
The interpreter, in turn, reads the scripts and executes its code. This is an interpreter that has permission to execute.
When the script is executed as ./script.py
, then the script is executed directly and therefore the script requires permission to execute. The interpreter used is set by the shebang line.
When the kernel detects that the first two bytes are #!
then it uses the rest of the string as an interpreter and passes the file as an argument. Please note that for this the file must have permission to execute. In the first case, we indirectly do what the kernel will do if we execute the script as ./script.py
In short, to execute method 1, the interpreter only needs read permission, but to execute it later you need to execute it directly
source share