Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > gnu.bash.bug > #15981
| From | Keith Thompson <Keith.S.Thompson@gmail.com> |
|---|---|
| Newsgroups | gnu.bash.bug |
| Subject | builtin echo vs /bin/echo appears to affect variable scope |
| Date | 2020-03-02 11:54 -0800 |
| Message-ID | <mailman.1922.1583178882.2412.bug-bash@gnu.org> (permalink) |
| References | <CAAHpriP0ZosuQ7=5_F294UxzLb5ynbECCeqFoQK8Ex_GDkFGgw@mail.gmail.com> |
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.
Repeat-By:
(/o/bin/bash is a symlink to bash 5.0.16 on my system.)
bad.bash:
```
#!/o/bin/bash
rm -f BASH_BUG_TEST*
i=0
printf '%s\n' should-be-0 should-be-1 should-be-2 | \
while read word ; do
/bin/echo hello \
> BASH_BUG_TEST_$((i++))_$word
done
ls -1 BASH_BUG_TEST*
rm -f BASH_BUG_TEST*
```
Output of bad.bash:
```
BASH_BUG_TEST_0_should-be-0
BASH_BUG_TEST_0_should-be-1
BASH_BUG_TEST_0_should-be-2
```
good.bash:
```
#!/o/bin/bash
rm -f BASH_BUG_TEST*
i=0
printf '%s\n' should-be-0 should-be-1 should-be-2 | \
while read word ; do
echo hello \
> BASH_BUG_TEST_$((i++))_$word
done
ls -1 BASH_BUG_TEST*
rm -f BASH_BUG_TEST*
```
Output of good.bash:
```
BASH_BUG_TEST_0_should-be-0
BASH_BUG_TEST_1_should-be-1
BASH_BUG_TEST_2_should-be-2
```
Back to gnu.bash.bug | Previous | Next | Find similar
builtin echo vs /bin/echo appears to affect variable scope Keith Thompson <Keith.S.Thompson@gmail.com> - 2020-03-02 11:54 -0800
csiph-web