I am confused with regards to the following - if I execute a script or run a program in the current shell by typing on its name

  1. it searches in the $PATH variable, to find the location of the executable
  2. once it finds it, does it run the executable in the current shell or will spawn a child shell/process to run the executable ?

I believe a new child process will be spawned to run the executable, but having said that, how does the child process know (the path to the executable) to run the executable ? — if I amended the PATH in the parent shell so that it can find the executable (using only PATH=$PATH:/newpath/to/executable) but not doing an export

e.g., — I did not do an export here
$ PATH=$PATH:/path/to/executable
$ executable 

When the parent shell finds the executable, what does it do next (spawn a child process? run the executable?). But again, how does the child process find the executable when the edited PATH environment is not exported to it

  • Is the first line of your shell script a #! line, or a line that is just #, or something else? – Mark Plotnick Aug 5 '15 at 17:36
  • it is actually sqlplus, a binary executable. when i type sqlplus, it is actually able to locate the executable. but it prompt me that some of the environment variables are not set properly - which leads me to think that the child process might need to access some public(env) variables, but again brought me to think - how does the child process even locate the executable in the 1st place ? does the child process run the executable or ? (which lead to this question) – Noob Aug 5 '15 at 17:52

It's not usually the parent that searches $PATH. It's usually the kernel.

Internally, spawning a new process is a fork (creates a new process but keeps running the current code; the parent and child each usually go into their own if branch) followed by an exec* call (replaces the current process image with a new process image) with optional stuff (such as filedescriptor redirections or signal settings) in between.

The exec* p members of the exec family (the exec family is just about doing the same thing in different ways) make the kernel search the $PATH environment variable when resolving an argument that doesn't have a slash in it.

Once the path is resolved, the kernel tries to run it as a binary, resorting to the shebang line if the former fails and the executable does have a shebang line. If the shebang line is used, the interpreter specified in it is run and passed the resolved path.


There is difference between subshell and child process. What you are asking is about subshell.

Whenever parent shell finds executable it spawns a subshell, subshell have access to all variables of parent shell. That's why variable PATH is accessible to subshell also.

When parent shell spawns a child process then access to variables is restricted in child process.

You can check by following command.

unset a; a=1
(echo "a is $a in the subshell")
sh -c 'echo "a is $a in the child shell"'

for more information refer link subshell info

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.