Bug 665 - read builtin doesn\'t work if its input comes from a pipe
Summary: read builtin doesn\'t work if its input comes from a pipe
Status: CLOSED NOTABUG
Alias: None
Product: Sisyphus
Classification: Development
Component: bash (show other bugs)
Version: unstable
Hardware: all Linux
: P4 major
Assignee: placeholder@altlinux.org
QA Contact:
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-02-23 17:50 MSK by imz
Modified: 2003-08-25 15:18 MSD (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description imz 2002-02-23 17:50:41 MSK
This command prints an empty line in bash-2.05-alt4:

a=\"\"; echo A | read a; echo $a

but it should print a line with A.

---

---
Strange that adding braces helps, these commands print A:

a=\"\"; echo A | { a=\"\"; read a; echo $a; }
a=\"\"; echo A | ( a=\"\"; read a; echo $a; )

Reading from the keyboard and not a pipe also works correctly.

All the three commands mentioned here work the same right way in zsh.

Comment 1 Dmitry V. Levin 2002-03-22 17:49:28 MSK
See /usr/share/doc/bash-2.05/FAQ:

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.
Comment 2 Dmitry V. Levin 2002-03-22 17:49:28 MSK
See /usr/share/doc/bash-2.05/FAQ:

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.