your while command probably runs in a sub-shell. note your use of a pipe
(seq 10 | while).

the reason that you see the same PID, is because '$$' gets expanded before
the fork that creates thw process with the 'while' command. so you see the
PID of the while process's parent process.

--guy

On Tue, 28 Jun 2005, Levy, Chen wrote:

> Date: Tue, 28 Jun 2005 17:00:57 +0000
> From: "Levy, Chen" <[EMAIL PROTECTED]>
> To: [email protected]
> Subject: Bash loop weirdness
>
> Hi list.
>
> Consider the following script:
>
> ----------
> #!/bin/bash
>
> n=5
> echo "init: $n"
>
> seq 10 | while read x ; do
>   n=$((n + 1))
>   echo -n "$$ $n : "
> done
>
> echo
> echo "after while: $n"
>
> for i in $(seq 10) ; do
>   n=$((n + 1))
>   echo -n "$$ $n : "
> done
>
> echo
> echo "after for: $n"
> echo "$$"
> ----------
>
> It's output is:
>
> ----------
> init: 5
> 14553 6 : 14553 7 : 14553 8 : 14553 9 : 14553 10 : 14553 11 : 14553 12 : 14553
> 13 : 14553 14 : 14553 15 :
> after while: 5
> 14553 6 :14553 7 : 14553 8 : 14553 9 : 14553 10 : 14553 11 : 14553 12 : 14553
> 13 : 14553 14 : 14553 15 :
> after for: 15
> 14553
> ----------
>
> The question is: "Way?"
>
> Why does the WHILE loop don't change the global $n where the FOR loop does.
> Note that in inside both loops the PID is the same.
>
> Cheers,
> Chen.
>

-- 
guy

"For world domination - press 1,
 or dial 0, and please hold, for the creator." -- nob o. dy


=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]

Reply via email to