bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

builtin echo vs /bin/echo appears to affect variable scope


From: Keith Thompson
Subject: builtin echo vs /bin/echo appears to affect variable scope
Date: Mon, 2 Mar 2020 11:54:24 -0800

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
```



reply via email to

[Prev in Thread] Current Thread [Next in Thread]