On 26.07.2022 15:25, kf wrote:
Obviously I've reduced and rewitten it to better point out the problem in this script. The question is, how should the variable "count" be declared so that it isn't treated as two, completely different variables (with exactly the same name)?
It has nothing to do with declaration or scope.
If you want to try to run it, it takes a filename as an commandline argument.
======================== #!/bin/bash
#How to declare the variable "count"??? count=0
cat $1 | while read line do count=$((count+1)) echo -n "$count " done
Right hand of pipe is executed in separate process (subshell), so any changes done there do not affect main shell process. You can redirect from file instead while read ... ... done < $1
echo echo File $1 has $count lines.
========================
Thanks.