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

Re: bash/read built-in function



On Sat, Nov 24, 2001 at 12:21:01AM +0100, martin f krafft wrote:
> bash-2.05$ echo "hello, world, this is ANOTHER test" | read a b
> bash-2.05$ echo $b
>   world, this is a test
> 
> is this because the pipe really just spawns another shell and thus the
> variable $b of the spawning shell is preserved? or is this a bug?

/usr/share/doc/bash/FAQ.gz

E4) If I pipe the output of a command into `read variable', why doesn't
    the output show up in $variable when the read command finishes?

This has to do with the parent-child relationship between Unix
processes.  It affects all commands run in pipelines, not just
simple calls to `read'.  For example, piping a command's output
into a `while' loop that repeatedly calls `read' will result in
the same behavior.

Each element of a pipeline runs in a separate process, a child of
the shell running the pipeline.  A subprocess cannot affect its
parent's environment.  When the `read' command sets the variable
to the input, that variable is set only in the subshell, not the
parent shell.  When the subshell exits, the value of the variable
is lost.
....

-- 
~\^o^/~~~ ~\^.^/~~~ ~\^*^/~~~ ~\^_^/~~~ ~\^+^/~~~ ~\^:^/~~~ ~\^v^/~~~ 
+  Osamu Aoki <debian@aokiconsulting.com>, GnuPG-key: 1024D/D5DE453D  +
+  My debian quick-reference, http://qref.sourceforge.net/quick/               +



Reply to: