help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Different methods of running scripts; subshells and exec


From: Michael Convey
Subject: Re: [Help-bash] Different methods of running scripts; subshells and execute privileges
Date: Mon, 24 Aug 2015 07:35:56 -0700

On Thu, Aug 20, 2015 at 5:31 AM, Greg Wooledge <address@hidden> wrote:

> exec-ing a program
> ​​
> causes all of your current non-environment variables
> to go away.  All of your functions, all of your code, go away.  The only
> things that are retained are the process ID, open file descriptors,
> environment variables, resource limits, privileges, and signal handlers
> (traps).
>
> If you execute a script in the REGULAR way (without exec), what you get
> is a child process.  This is not a "subshell" -- see below.  A child
> process is the result of a fork() followed by an exec() in the child.

The child process has a brand new PID, and inherits the parent's
> environment variables and open file descriptors, but runs independently.
>

I've read your reply many times and I just want to make sure that my
understanding is correct. Please confirm that my explanation below is
correct for any of the following script execution methods for bash:

   - ​​
   bash /path/script
   - sh /path/script
   - ​​
   /path/script.sh

The fork() creates a new process -- a subshell, which is an identical clone
of the original process -- but with a different PID​. Then, the exec()
replaces that new process with an even newer process, but with the same PID
as the forked process. This process is no longer a subshell because as you
stated above, exec "causes all of your current non-environment variables
to go away. All of your functions, all of your code, go away.  The only
things that are retained are the process ID, open file descriptors,
environment variables, resource limits, privileges, and signal handlers
(traps).
​

Is this accurate? ​



> A subshell is also a child process but it has a very specific meaning
> in bash programming.  A subshell is the result of a fork() NOT followed
> by an exec().  A subshell inherits copies of everything the parent
> has -- environment variables, NON-environment variables, functions,
> loop states, everything.  It's a perfect clone.
>
> Subshells may be created explicitly by putting ( ) around a command,
> or more commonly
> ​...
>

​So is it possible to run a script in a true subshell? If so, is the
following the only way to to do this? ​

   - ​
   bash (/path/script)
   - sh (/path/script)
   - ​
   /path/script()


reply via email to

[Prev in Thread] Current Thread [Next in Thread]