[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: builtin echo vs /bin/echo appears to affect variable scope
From: |
Chet Ramey |
Subject: |
Re: builtin echo vs /bin/echo appears to affect variable scope |
Date: |
Mon, 2 Mar 2020 15:01:11 -0500 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 |
On 3/2/20 2:54 PM, Keith Thompson wrote:
> From: kst
> To: bug-bash@gnu.org
> Subject: echo vs /bin/echo appears to affect variable scope
>
> Configuration Information [Automatically generated, do not change]:
> Machine: x86_64
> OS: linux-gnu
> Compiler: gcc
> Compilation CFLAGS: -g -O2 -Wno-parentheses -Wno-format-security
> uname output: Linux bomb20 4.15.0-88-generic #88-Ubuntu SMP Tue Feb 11
> 20:11:34 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
> Machine Type: x86_64-pc-linux-gnu
>
> Bash Version: 5.0
> Patch Level: 16
> Release Status: release
>
> Description:
> The original test case was a small script in this answer on Stack Overflow:
> https://stackoverflow.com/a/60480960/827263
>
> I've narrowed it down to two scripts that differ only in
> using "echo" vs. "/bin/echo". With the built-in "echo",
> $i is correctly incremented. With the external "/bin/echo",
> $i appears to be 0 on each iteration.
>
> Moving the "((i++))" into a separate command also works around
> the bug.
It's not a bug. That expansion is performed as part of a redirection;
redirections are performed in subshells when the simple command they
affect is run in a subshell.
--
``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/