For example, if I do
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="612e31210d0e02000d090e1215">[email protected]</a> executable]$ cat garbage lalala trololol [<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ad5cadaf6f5f9fbf6f2f5e9ee">[email protected]</a> executable]$ chmod +x garbage [<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2c637c6c40434f4d4044435f58">[email protected]</a> executable]$ ./garbage ./garbage: line 1: lalala: command not found ./garbage: line 2: trololol: command not found
Bash seems to be trying to interpret this “executable” as a script. However, there are two instances where this clearly does not happen: when the file begins with a #!, and ELF files. Are there any more? Is there a comprehensive documentation of this somewhere?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Expanding on my previous comment on another answer, the kernel contains seven binary loaders (look for files starting with binfmt_ there, or read the binfmt-specific Kconfig):
a.out(which is currently on a stay of execution);- ELF;
- FDPIC ELF (on ARM, MMU-less SuperH, and C6x);
em86(on Alpha);- flat binaries (on MMU-less systems, or ARM, or M68k);
- scripts;
- the almighty
binfmt_misc(see also What kinds of executable formats do the files under /proc/sys/fs/binfmt_misc/ allow?).
These are what determine the types of executable files that the kernel can execute. binfmt_misc in particular allows many other binaries to be handled by the kernel (at least, from the perspective of the process calling one of the exec functions).
However this doesn’t cover the whole story, since the C library and shells themselves are also involved. POSIX now requires that the execlp and execvp functions, when they encounter an executable which the kernel can’t run, try running it using a shell; see the rationale here for details.
The way all this interacts to provide the behaviour you’re seeing is detailed in What exactly happens when I execute a file in my shell? and Which shell interpreter runs a script with no shebang?
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0