[Top][All Lists]

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

Re: Why is this executed inside a subshell?

From: Chet Ramey
Subject: Re: Why is this executed inside a subshell?
Date: Mon, 27 Nov 2006 14:59:12 -0500
User-agent: Thunderbird (Macintosh/20061025)

Com MN PG P E B Consultant 3 wrote:
> Consider the following program:
> #!/usr/local/bin/bash --norc
> export VAR=A
> function setvar
> {
>   VAR=B
>   echo X
> }
> V=$(setvar)
> echo $VAR
> When I execute it, I get as result "A", not "B", as I had expected.

Posix requires that command substitution be executed in a "subshell
environment."  It forbids changes to the subshell environment affecting
the calling shell's environment.  The way to do that is to run command
substitutions in an actual subshell and communicate via pipes.


``The lyf so short, the craft so long to lerne.'' - Chaucer
                       Live Strong.  No day but today.
Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/

reply via email to

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