27 Jul
2022
27 Jul
'22
17:35
On 7/26/22 5:56 PM, Michael Hamilton wrote:
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. }
Nice. Thanks. It's always good to have options.