help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] confusion of subshell


From: Greg Wooledge
Subject: Re: [Help-bash] confusion of subshell
Date: Fri, 11 Jan 2013 11:40:23 -0500
User-agent: Mutt/1.4.2.3i

On Sat, Jan 12, 2013 at 12:16:27AM +0800, Ma Shimiao wrote:
> On 01/12/2013 12:10 AM, Ma Shimiao wrote:
> >hello,everyone:
> >
> >I'm learning to use bash. There is a question which confuses me for a 
> >long time.
> >There are many places mentions subshell in bash's ManPage.
> >But, what is subshell actually? Is it to exec a new bash script ?
> >Could someone help me ?
> and how to create subshells?

The formal definition is in the man page under COMMAND EXECUTION ENVIRONMENT:

      Command substitution, commands grouped with parentheses, and
      asynchronous commands are invoked in a subshell environment that is a
      duplicate of the shell environment, except that traps caught by the
      shell are reset to the values that the shell inherited from its parent
      at invocation.  Builtin commands that are invoked as part of a
      pipeline are also executed in a subshell environment.  Changes made to
      the subshell environment cannot affect the shell's execution
      environment.

Basically, a subshell is a Unix fork().  The child process gets a copy
of all the parent's variables, open file descriptors and so on, but any
changes that the child makes are not visible to the parent.

With a separate script, the child would NOT get a copy of the parent
shell's variables, except for ones that have been exported.

See also http://mywiki.wooledge.org/SubShell



reply via email to

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