0

When I code as:

args=()
for ((i=0; i<5; i++))
do
    args+=("o")
done

echo "${args[@]}"

it prints

o o o o o

as I expected.

But, when I change for statement to while read as:

args=()
cat $file | while read -r line
do
    args+=("o")
done

echo "${args[@]}"

args doesn't contain anything and prints nothing. I declare args as global.

Why does it happen? And, how can I use array while reading lines of a file?

+ Update

I changed my code to:

while read line
do
    # ...
done < $file

and it works. Why does it work unlike above case?

osflw
  • 51
  • 5

0 Answers0