Array and Loop Problem

I've got this problem, if I modify an array in the loop and print it, everything is fine as long as I stay in the loop. But, when I print it outside the loop, nothing happens... How can I solve this problem?

Here I prepared a sample for you to see my problem;

zgrw@Rain:~$ cat test
asd
123
13
1234
zgrw@Rain:~$ cat test.sh
#!/bin/bash

declare -a asd
c=0
cat test | while read line; do
        asd[$c]=$line
        echo ${asd[@]}
        ((c++))
done

echo "I wish you here"
echo ${asd[@]}

Output is;

zgrw@Rain:~$ ./test.sh 
asd
asd 123
asd 123 13
asd 123 13 1234
I wish you here

zgrw@Rain:~$ 
#!/bin/bash

declare -a asd
c=0
while read line; do
        asd[$c]=$line
        echo ${asd[@]}
        ((c++))
done < test

echo "I wish you here"
echo ${asd[@]}

wow thanks a lot :slight_smile:

why does it work like this but not with cat?

'cat whatever |' creates a subprocess for the 'while read' - the variables within the subprocess/scope stay local and are not accessible outside the subprocess/scope.

hmm interesting. thank you very much