Looping through lines in a file in bash, without using stdin Looping through lines in a file in bash, without using stdin shell shell

Looping through lines in a file in bash, without using stdin


You must open as a different file descriptor

while read p <&3; do    echo "$p"    echo 'Hit enter for the next one'    read xdone 3< list.txt

Update: Just ignore the lengthy discussion in the comments below. It has nothing to do with the question or this answer.


I would probably count lines in a file and iterate each of those using eg. sed. It is also possible to read infinitely from stdin by changing while condition to: while true; and exit reading with ctrl+c.

line=0 lines=$(sed -n '$=' in.file)while [ $line -lt $lines ]do    let line++    sed -n "${line}p" in.file    echo "Hit enter for the next ${line} of ${lines}."    read -s xdone

AWK is also great tool for this. Simple way to iterate through input would be like:

awk '{ print  $0; printf "%s", "Hit enter for the next"; getline < "-" }' file


As an alternative, you can read from stderr, which by default is connected to the tty as well. The following then also includes a test for that assumption:

(tty -s <& 2|| exit 1while read -r line; do    echo "$line"    echo 'Hit enter'    read x <& 2done < file)