Results 1 to 7 of 7
When the shell creates a new process to execute a command, how does it know that the file is executable?
If it is executable, how does it distinguish between a ...
- 02-04-2009 #1Just Joined!
- Join Date
- Aug 2008
- Posts
- 49
shell script v/s executable
When the shell creates a new process to execute a command, how does it know that the file is executable?
If it is executable, how does it distinguish between a shell script and a file produced by a compilation?
what is the correct sequence for checking the above cases?
- 02-04-2009 #2Linux Newbie
- Join Date
- Jul 2008
- Posts
- 181
A file is executable if its executable flag is set.
As far as I know, the shell calls exec or another function from the same family. The kernel then checks the header of the file (first 128 bytes) to see if it is a registered binary format (binfmt). If so, it invokes the necessary program to execute the file (e.g. virtual machine or emulator), or process it (linker). The header of the file may itself contain the name of the necessary program (#! notation).
- 02-04-2009 #3Just Joined!
- Join Date
- Aug 2008
- Posts
- 49
ya fine, if executable flag is set, we can execute.
But basically my question is if i have written some file called "abc". contents are
ls -l | wc -l
Here i am not specifying anything like #!/bin/sh in the header. How it automatically recognize its a shell script file not executable.
cc -o abc file.c
Here i can understand that, "abc" is in ELF format. but in previous condition i didn't get?
- 02-04-2009 #4Registered Linux user #270181
TechieMoe's Tech Rants
- 02-05-2009 #5
I think that this might be a different question.
Let's assume that I have this "abc" file with the contents that you mention. If I now run the command "./abc", I will get an error that the script is not executable.
However, if I run the command "bash abc", it does work. Why is this? Because bash is an executable program, and we have given it a file to use as its input. Because we are not executing the "script" directly (but rather passing it as input to an interpreter), it appears that the script executes.
Does this help?DISTRO=Arch
Registered Linux User #388732
- 02-06-2009 #6Just Joined!
- Join Date
- Aug 2008
- Posts
- 49
Ya i understood, but to read executable header i can use "readelf". But to read shell script(execution flag set) header, what command to use?
Somebody( burschik) said it will recognize through header?
- 02-06-2009 #7Linux Newbie
- Join Date
- Jul 2008
- Posts
- 181
Yes, the kernel recognizes the binfmt by reading the header. I don't know whether there is a command line program for reading the headers of (potentially) executable files, but you can look at the kernel code, here, for example:
LXR linux/fs/binfmt_script.c


Reply With Quote
