[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: unexpected behavior with read
From: |
Chet Ramey |
Subject: |
Re: unexpected behavior with read |
Date: |
Sun, 4 Mar 2018 20:55:04 -0500 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 |
On 3/4/18 6:15 PM, Zach Hadgraft wrote:
> Bash Version: 4.4
> Patch Level: 19
> Release Status: release
>
> Description:
> global variable assignments fail when part of a sequence that
> includes read and begins with a function invoked by command substitution
> read can be invoked by another function, or invoked by another
> command and the result is the same
>
> Repeat-By:
>
> """
> #!/bin/bash
>
>
> r=
>
> f ()
> {
> if [ -z "$r" ]; then
> r="x"
> read
> fi
> echo "$r"
> }
>
> echo "$(f)"
> echo "$(f)"
> """
What do you expect to happen, and what happens instead? When I run this
script, the `read' gets satisfied when you enter a line of text followed
by a newline, and `x' is assigned to r:
line
x
line
x
Since command substitution happens in a subshell, and subshells can't
affect their parent's environment, `r' isn't going to change in the
calling shell.
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet@case.edu http://tiswww.cwru.edu/~chet/