Read input in bash inside a while loop

bash while loop
bash while read -r
while read line bash
bash read
bash read input
do while loop in shell script
while loop in shell script with sleep
bash read input in loop

I am having a bash script which is something like following,

cat filename | while read line
do
    read input;
    echo $input;
done

but this is clearly not giving me the right output as when I do read in the while loop it tries to read from the file filename because of the possible I/O redirection.

Any other way of doing the same?


Read from the controlling terminal device:

read input </dev/tty

more info: http://compgroups.net/comp.unix.shell/Fixing-stdin-inside-a-redirected-loop

Use read as a prompt inside a while loop driven by read?, What is needed is to redirect one input to some other fd (file descriptor). Valid in ksh, bash and zsh: while read -u 7 a b c  If you are using the direct output of a command to feed a while loop in BASH, you may still want to take user input inside that loop. For example, if you need the user to confirm each line by pressing <ENTER>. Take a simple example like this: grep one test.txt | while read -r line; do echo "Processing $line" read done


You can redirect the regular stdin through unit 3 to keep the get it inside the pipeline:

{ cat notify-finished | while read line; do
    read -u 3 input
    echo "$input"
done; } 3<&0

BTW, if you really are using cat this way, replace it with a redirect and things become even easier:

while read line; do
    read -u 3 input
    echo "$input"
done 3<&0 <notify-finished

Or, you can swap stdin and unit 3 in that version -- read the file with unit 3, and just leave stdin alone:

while read line <&3; do
    # read & use stdin normally inside the loop
    read input
    echo "$input"
done 3<notify-finished

Bash: reading input within while read loop doesn't work, The problem is that both read line and read response expects (and gets) data from stdin . This question on SO explains some of it with a link to even more  Bash: Read File Line By Line – While Read Line Loop. The while loop is the best way to read a file line by line in Linux. If you need to read a file line by line and perform some action with each line – then you should use a while read line construction in Bash, as this is the most proper way to do the necessary.


Try to change the loop like this:

for line in $(cat filename); do
    read input
    echo $input;
done

Unit test:

for line in $(cat /etc/passwd); do
    read input
    echo $input;
    echo "[$line]"
done

For and Read-While Loops in Bash, A read-while loop will preserve the words within a line: user@host:~$ The data input source, each URL in urls.txt , isn't really being filtered here. Instead, a  Piping into read-while. That said, a loop itself can be implemented as just one more filter among filters. Take this variation of the read-while loop, in which the result of echo | grep is piped, line by line, into the while loop, which prints to stdout using echo, which is redirected to the file named some.txt:


It looks like you read twice, the read inside the while loop is not needed. Also, you don't need to invoke the cat command:

while read input
do
    echo $input
done < filename

Bash: Reading input from the console while looping over output of , Bash: Reading input from the console while looping over output of command. If you are using the direct output of a command to feed a while loop in BASH, you may still want to take user input inside that loop. For example, if you need the user to confirm each line by pressing <ENTER>. The bash read command is very convenient for: read -p to prompt the user and capture input from the user while read loop to iterate through the lines of a file. However, I'm having issues attempt Stack Overflow


I have found this parameter -u with read.

"-u 1" means "read from stdin"

while read -r newline; do
    ((i++))
    read -u 1 -p "Doing $i""th file, called $newline. Write your answer and press Enter!"
    echo "Processing $newline with $REPLY" # united input from two different read commands.
done <<< $(ls)

read command (input) inside the while loop, The below works fine on Linux bash or ksh shell . while IFS= read -r dirpath ; do echo "Hi" done <<<"$var" However, any such while loop that reads the input  But the code has the distinct problem that it compares the value of input before reading it. You need to read the input, then compare it to something. Fortunately, this is not hard to do, because you can have multiple commands in the while condition. The other problem seems to be that you never break out of the inner loop.


Bash - How to do a "read -p" inside a while loop?, The standard input to your loop is redirected from file.txt, so both read commands in the loop are reading from that file. If you want to have the outer read read from​  Instead of controlling the while loop with a condition, we are using input redirection (< "$file") to pass a file to the read command, which controls the loop. The while loop will run until the last line is read. When reading file line by line, always use read with the -r option to prevent backslash from acting as an escape character.


The while loop, As soon as the CONTROL-COMMAND fails, the loop exits. bin/bash # This script opens 4 terminal windows. i="0" while [ $i -lt 4 ] do xterm literature drugs education topics echo echo -n "Make your choice: " read topic of user input, which is tested before it is processed: if input is not within range, a message is printed. For every iteration of the while loop, read reads one word (a single file name) and puts that value into the variable file, which we specified as the last argument of the read command. When there are no more file names to read, read returns false, which triggers the end of the while loop, and the command sequence terminates.


Bash while Loop, Instead of controlling the while loop with a condition, we are using input redirection ( < "$file" ) to pass a file to the read  hi! i need to do a ksh script that uses a wile loop that is fed form below while read line do some things done < myfile inside the while loop i need to read user input to ask the user what he wants to do, but "read" reads the file, and not the standard input while read line do (2 Replies)