Skip to main content
formatting
Source Link
StackzOfZtuff
  • 3.3k
  • 1
  • 31
  • 31

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 attempting to do both simultaneously.

For example:

#!/bin/bash

while read item
do

    echo Item: $item
        
    read -p "choose wisely: " choice
        
    echo You still have made a $choice.
    
done < /tmp/item.list 
#!/bin/bash

while read item
do
    echo Item: $item
    read -p "choose wisely: " choice
    echo You still have made a $choice.
done < /tmp/item.list 

Rather than blocking and standing by for the user to enter a choice, bash is populating $choice$choice with the next item in the item.listitem.list file.

Does bash support doing a read nested within a read loop?

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 attempting to do both simultaneously.

For example:

#!/bin/bash

while read item
do

    echo Item: $item
        
    read -p "choose wisely: " choice
        
    echo You still have made a $choice.
    
done < /tmp/item.list 

Rather than blocking and standing by for the user to enter a choice, bash is populating $choice with the next item in the item.list file.

Does bash support doing a read nested within a read loop?

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 attempting to do both simultaneously.

For example:

#!/bin/bash

while read item
do
    echo Item: $item
    read -p "choose wisely: " choice
    echo You still have made a $choice.
done < /tmp/item.list 

Rather than blocking and standing by for the user to enter a choice, bash is populating $choice with the next item in the item.list file.

Does bash support doing a read nested within a read loop?

Revised the example to eliminate a problem unrelated to the question.
Source Link
ddoxey
  • 2.1k
  • 2
  • 19
  • 25

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 attempting to do both simultaneously.

For example:

#!/bin/bash

while read item
do

    while [[ "$choice" != "choice" ]]
    do

        echo Item: $item
        
        read -p "choose wisely: " choice
        
        echo You still have made a $choice.
        
    done
    
done < /tmp/item.list 

Rather than blocking and standing by for the user to enter a choice, bash is populating $choice with the next item in the item.list file.

Does bash support doing a read nested within a read loop?

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 attempting to do both simultaneously.

For example:

#!/bin/bash

while read item
do

    while [[ "$choice" != "choice" ]]
    do

        echo Item: $item
        
        read -p "choose wisely: " choice
        
        echo You still have made a $choice.
        
    done
    
done < /tmp/item.list 

Rather than blocking and standing by for the user to enter a choice, bash is populating $choice with the next item in the item.list file.

Does bash support doing a read nested within a read loop?

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 attempting to do both simultaneously.

For example:

#!/bin/bash

while read item
do

    echo Item: $item
        
    read -p "choose wisely: " choice
        
    echo You still have made a $choice.
    
done < /tmp/item.list 

Rather than blocking and standing by for the user to enter a choice, bash is populating $choice with the next item in the item.list file.

Does bash support doing a read nested within a read loop?

Source Link
ddoxey
  • 2.1k
  • 2
  • 19
  • 25

Does bash support doing a read nested within a read loop?

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 attempting to do both simultaneously.

For example:

#!/bin/bash

while read item
do

    while [[ "$choice" != "choice" ]]
    do

        echo Item: $item
        
        read -p "choose wisely: " choice
        
        echo You still have made a $choice.
        
    done
    
done < /tmp/item.list 

Rather than blocking and standing by for the user to enter a choice, bash is populating $choice with the next item in the item.list file.

Does bash support doing a read nested within a read loop?