# read command in while

**URL:** <https://community.unix.com/t/read-command-in-while/229935>\
**Category:** Shell Programming and Scripting\
**Created:** [February 2, 2009, 3:14am UTC](https://community.unix.com/t/read-command-in-while/229935 "2009-02-02T03:14:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![avi.skynet](https://community.unix.com/letter_avatar/avi.skynet/32/6_22734026db12803ebb3e059622ac3534.png) [@avi.skynet](https://community.unix.com/u/avi.skynet)\
**Post date:** [February 2, 2009, 3:14am UTC](https://community.unix.com/t/read-command-in-while/229935/1 "2009-02-02T03:14:38Z")

</div>

hi all  
iam not able use read command in the while loop in the following program

while read line  
do  
echo $line  
echo "enter name"  
read name  
echo "your have entered $name"  
done \< work.txt  
THE READ COMMAND INSIDE THE WHILE LOOP IS NOT WORKING, IS ANY OTHER WAY TO SOLVE THIS PROBLEM

thanks to every one 😕  
Avinash

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [February 2, 2009, 4:17am UTC](https://community.unix.com/t/read-command-in-while/229935/2 "2009-02-02T04:17:22Z")

</div>

Since read gets that file work.txt spooled in, it get's it's answers from it. Anyway maybe explain what you want to do - maybe there is an alternative way. I see no useful use :eek: of work.txt in this script.

---

<div class="post-metadata">

**Author:** ![avi.skynet](https://community.unix.com/letter_avatar/avi.skynet/32/6_22734026db12803ebb3e059622ac3534.png) [@avi.skynet](https://community.unix.com/u/avi.skynet)\
**Post date:** [February 2, 2009, 7:55am UTC](https://community.unix.com/t/read-command-in-while/229935/3 "2009-02-02T07:55:16Z")

</div>

hi thanks for the reply..  
my work is done wit my code. but iam debugging the code.. so i just want to know what is happening in side the while loop one by one ... so that i can make my code efficient...

any way thanks reading and replying to my post... :b:

---

<div class="post-metadata">

**Author:** ![angheloko](https://community.unix.com/user_avatar/community.unix.com/angheloko/32/1030_2.png) [@angheloko](https://community.unix.com/u/angheloko)\
**Post date:** [February 2, 2009, 8:00am UTC](https://community.unix.com/t/read-command-in-while/229935/4 "2009-02-02T08:00:59Z")

</div>

Hope this helps:

```nohighlight
TTY=`tty`

while read line
do
echo $line
echo "enter name"
read name < $TTY
echo "your have entered $name"
done < work.txt

```

---

<div class="post-metadata">

**Author:** ![Perderabo](https://community.unix.com/user_avatar/community.unix.com/perderabo/32/27_2.png) [@Perderabo](https://community.unix.com/u/Perderabo)\
**Post date:** [February 2, 2009, 9:52am UTC](https://community.unix.com/t/read-command-in-while/229935/5 "2009-02-02T09:52:57Z")

</div>

Or just:  
TTY=`/dev/tty`

---

<div class="post-metadata">

**Author:** ![quirkasaurus](https://community.unix.com/user_avatar/community.unix.com/quirkasaurus/32/1325_2.png) [@quirkasaurus](https://community.unix.com/u/quirkasaurus)\
**Post date:** [February 2, 2009, 10:03am UTC](https://community.unix.com/t/read-command-in-while/229935/6 "2009-02-02T10:03:27Z")

</div>

just wanted to add that the reason it's not working is because  
you are reading from stdin in 2 places:

while read....

and read...

that's what's causing the problem.

---

<div class="post-metadata">

**Author:** ![avi.skynet](https://community.unix.com/letter_avatar/avi.skynet/32/6_22734026db12803ebb3e059622ac3534.png) [@avi.skynet](https://community.unix.com/u/avi.skynet)\
**Post date:** [February 2, 2009, 1:39pm UTC](https://community.unix.com/t/read-command-in-while/229935/7 "2009-02-02T13:39:30Z")

</div>

First of all Thanks for all the reply

ill work out with mr angheloko code which he has sent

pls work it out is there any possibilities to read twise stdin .. 😃

---

<div class="post-metadata">

**Author:** ![cfajohnson](https://community.unix.com/user_avatar/community.unix.com/cfajohnson/32/16669_2.png) [@cfajohnson](https://community.unix.com/u/cfajohnson)\
**Post date:** [February 2, 2009, 1:43pm UTC](https://community.unix.com/t/read-command-in-while/229935/8 "2009-02-02T13:43:33Z")

</div>

> [@perderabo](#):
>
> Or just:  
> TTY=`/dev/tty`

```plaintext
$ TTY=`/dev/tty`
bash: /dev/tty: Permission denied

```

I think you meant:

```plaintext
TTY='/dev/tty'

```

Or

```plaintext
TTY=`tty`

```
