Fwd: variable scope bug in while loop when using pipe

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

 



---------- Forwarded message ----------
From: z7z8th <ybzhao1989@xxxxxxxxx>
Date: 2013/1/18
Subject: variable scope bug in while loop when using pipe
To: dash@xxxxxxxxxxxxxxx


$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Aug 21 08:11 /bin/sh -> dash*

The script did not work as what I wanted? Do you think it's a bug?
>
> #!/bin/sh
>
> abc=99
>
> echo "1\n2" | while read line; do
>     echo "line=$line"
>     echo "abc=$abc"
>     abc=123
> done
>
> echo "abc=$abc"
>

The output looks like below:
>
> line=1
> abc=99
> line=2
> abc=123
> abc=99


I think the last line should be abc=123

If the while loop is not after the pipe, it works as what I wanted.

> #!/bin/sh
>
> abc=99
>
> line=1
> while [ $line -lt 3 ]; do
>     echo "line=$line"
>     echo "abc=$abc"
>     abc=123
>     line=$((line+1))
> done
>
> echo "abc=$abc"


the variable abc is 123 in the end.

Then I found this article talking about this problem.
http://www.kilala.nl/Sysadmin/index.php?id=741
It says that old bash has this problem.
But I found bash-4.2.37 and dash-0.5.7 has this problem too.

I'm using Debian testing.
Linux 3.2.0-4-amd64 #1 SMP Debian 3.2.35-2 x86_64 GNU/Linux
--
To unsubscribe from this list: send the line "unsubscribe dash" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux