Home > Mobile >  Why do most programming languages not respect the executable flag on a file?
Why do most programming languages not respect the executable flag on a file?

Time:06-21

Why do most programming languages (Python, Bash, etc) not respect the executable flag on programs?

E.g. if I have a Python script that doesn't have the executable flag enabled, running that program in the shell directly yields an error, yet running it with Python does not:

$ ./test.py
bash: ./test.py: Permission denied
$ python3 test.py
... program output ...

Why is the x flag ignored so often?

Does the lack of an x flag not denote that a file should not be run/executed/interpreted?

CodePudding user response:

Python is simply interpreting the file, not executing it.

The only thing being executed here is python itself.


EDIT

The important thing to understand is that in the case of bash, it will actually execute the program you give it by either looking for a shebang line or simply fork a new process for the program and wait until the forked child is finished. This is why it throws an error if the executable bit is not set.

In the case of python, python is not executing anything, it automatically assumes the file is a python script, and then it interprets it. Therefore, python does not care if your script is executable or not, the only bit it cares about is if the file is actually a file and is readable.

CodePudding user response:

Let look at $ python3 test.py, I believe python3 is executable, test.py is a parameter pass to python3 and python3 find the file test.py under the same folder, the test.py has r permission to let python3 read the content.

CodePudding user response:

Why do most programming languages (Python, Bash, etc) not respect the executable flag on programs?

[...]

Does the lack of an x flag not denote that a file should not be run/executed/interpreted?

Correct: executable permission does not have the broad meaning you are trying to attribute to it. As far as the system is concerned, execute permission on a given file for a given user means only that the system will allow that user to attempt to execute that file as a command. It has no bearing on the behavior of programs (such as shells and language interpreters) that read the file as data.

To be sure, it would be possible for shells and the like to implement a parallel restriction themselves, but there's not much point to that. If a user can read the file then they can make their own copy of it and assign whatever permissions they want, so as to enable them to run that copy. Instead, shells and such tend to take a more user-friendly approach: they attempt to do what is asked of them, inasmuch as they can do.

CodePudding user response:

There seems to be a slight misconception here about how stuff on Linux gets executed.

Anything typed in a shell will be handed to the kernel after the ENTER.

If it's an elf-executable it will be run directly.

If it has a shebang line the kernel will (try to) use whatever follows the !# as an interpreter to execute the rest of the file.

If it's not an executable and doesn't have a shebang line you get the error you're seeing, despite the executable flag.

The canonical solution to your problem is to prepend your python code in your script with something like:

#!/usr/bin/env python3
  • Related