Someone asked a question on Ask Ubuntu about why they got permission denied
when they entered in a Bash shell
/etc/profile
I know this is because the file has no execute permission. Even root cannot execute it (sudo /path/to/file/with/no/execute/bits
fails with the uninformative error sudo /path/to...: command not found
). I also know that root can enter a directory with no execute bits, so the absolute prohibition on executing non-executable files seems special. In chat, Eliah Kagan opined that the reason root would not be able to execute non-executable files was to protect root (presumably, from accidentally executing dangerous code).
I briefly wondered why anyone would want to execute /etc/profile
and thought that if anyone did want to execute it, they would probably actually want to source
it (because it is a configuration file that sets environment and shell variables). I then realised that execute permission is not needed to source
any regular file. But source
executes the file in the current shell! The file can contain any command, and . file
will go right ahead and execute it.
If execute permission is restricted to prevent accidental execution of potentially dangerous code, why is it possible to execute non-excutable files using the source
command?
. /etc/profile
as "accidental". it's your responsibility to not burn out your remaining eye with the laser pointer. – cas Jan 25 '18 at 13:21.
withsource
inbash
. – Kusalananda Jan 25 '18 at 13:22