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 c...@case.edu http://tiswww.cwru.edu/~chet/