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.

Attachment: pgpr9gSXJa00K.pgp
Description: PGP signature

Reply via email to