A variant of this question has been asked a few times (here and here, for instance) but I'm afraid that the answers either didn't fully capture my question here or else perhaps assumed more than I know.
I will ask my question by example but, roughly, what I am trying to understand is (1) how the shell discerns between executables and scripts and, if it does, (2) if there are any differences in terms of what happens next when it does.
Suppose in my working directory is a shell script script
and an executable (I am taking "exeecutable" to mean "machine code" in binary, but perhaps that's not right) exe
. Let's suppose I'm interacting with the bash shell, and let's suppose script
is such that both bash and tcsh can run it. Further, assume that a priori the first line does not begin with a shebang #!...
.
Suppose I enter
script
at the command line prompt. How does bash figure out that this is a script and not an executable, and what does it do once it figures it out? (I think the answer is "create a new process which is, in particular, a new shell (i.e. a subshell) which is bash in this case (because I have no shebang) and then execute the commands in the script therein", but I am not sure.)Now suppose I enter
exe
at the command line prompt. How does bash figure out that this is an executable and not a script, and what does it do once it figures it out? (I think the answer is "create a new process and wait for the executable to finish", but I am not sure.)Now suppose I modify
script
to include#! /bin/tcsh
in the first line. How does bash figure out that this is a script (does the shebang change anything from the answer to (1)?) and not an executable, and what does it do once it figures it out? (I think the answer is "create a new process which is, in particular, a new shell (i.e. a subshell) which is tcsh in this case (because I the shebang) and then execute the commands in the script therein", but I am not sure.)
execve()
with the info you mentioned? – EE18 Feb 04 '24 at 21:43