I have a shell script like this , i am reading a file and counting the line number. i got logs like 1,2,3,4.. etc from inside processsNumber function
#!/bin/bash
number=0;
processNumber () {
((number++));
echo "$number";
}
grep -E '*' readme.txt | while read -r line ; do
processNumber "$line";
done;
echo "And at last $number";
But it logs
"And at last 0" , but i was expecting the last line number.
Why its like that ? is it because gres reads a file a assync call hence echo has to be a callback to that.
Or is that global variable changes cannot be tracked outside the function.?
How to fix this with out changing the grep and pipe ?
NOTE: my target is not find the number of lines in the file , but to understand this