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 08:44:16 -0700

On Mon, Aug 24, 2015 at 7:55 AM, Greg Wooledge <address@hidden> wrote:

> On Mon, Aug 24, 2015 at 07:35:56AM -0700, Michael Convey wrote:
> >    - ??????
> >    bash /path/script
> >    - sh /path/script
> >    - ??????
> >    /path/script.sh
>
> I literally cannot even READ the first and fourth lines of this.
> Whatever non-ASCII characters you used are not being rendered in my
> poor old Latin-1 terminal.
>
> If you type "bash" then the kernel runs bash.
>
> If you type "sh" then the kernel runs sh.
>
> If you type the scriptname, the kernel runs the script.  Running a script
> means it launches the interpreter in the shebang line.
>

​Okay, here is my message again with all formatting removed - let me know
if this is legible: ​

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]