The problem is that you add the elements in a subshell. To elaborate:
command1 | command2
causes command2
to be executed in a subshell, which is a separate execution environment. This implies that the variables set in command2
are not available to the current shell, whose execution environment is not affected. You could instead use process substitution to achieve the same:
while read line; do
...
done < <(ls -l)
Please note that parsing ls is not recommended; try using find
instead.