26 Jul
2022
26 Jul
'22
21:56
On Wednesday 27 July 2022, kf wrote:
#!/bin/bash
#How to declare the variable "count"??? count=0
cat $1 | while read line do count=$((count+1)) echo -n "$count " done
echo echo File $1 has $count lines.
As other have explained, the pipe receiver runs in it's own subprocess. If you were in a situation where you did really have to read from a pipe, you could also code it as follows: #!/bin/bash cat $1 | { count=0 while read line do count=$((count+1)) echo -n "$count " done echo echo File $1 has $count lines. }