How can I read line by line using for loop?
what I've tried:
hh=$(echo -e "\n1\n2\n3\n4\n")
IFS=$'\n';
for r in "$hh"; do echo $r; done
1 2 3 4
echo -e "$hh"
1
2
3
4
Use a while loop:
$ while read -r r; do echo $r; done <<< "$hh"
1
2
3
4
The correct answer is, you don't read line-by-line with a for loop. Use a while loop instead with the read builtin:
while IFS= read -r line; do
echo "$line"
done <<< "$hh"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With