May I doubt that? Running that code as is will print In line no $line_no . Changing the single quotes around the echo command to double quotes will print
In line no 1 a 5
2 4 4 3
, as those enable variable expansion. $line holds 1 2 3 from the first read , $line_no contains
1 a 5
2 4 4 3
, i.e. NR and $0 from the rest of the input file redirected from stdin. The while loop is exited after the first iteration. This is because read and awk both read from the input file, and awk loops through it until it's exhausted.
Use tools consistently, either awk only as proposed by vgersh99, or pure shell like
while read line; do echo "in line $((++CNT))"; done < file1
in line 1
in line 2
in line 3
Here an additional variable needs to be deployed as the shell doesn't provide a line count for stdin by itself.