# Bash : While Loop behavior

**URL:** <https://community.unix.com/t/bash-while-loop-behavior/350367>\
**Category:** UNIX for Advanced & Expert Users\
**Created:** [February 12, 2015, 12:11pm UTC](https://community.unix.com/t/bash-while-loop-behavior/350367 "2015-02-12T12:11:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![popeye](https://community.unix.com/user_avatar/community.unix.com/popeye/32/884_2.png) [@popeye](https://community.unix.com/u/popeye)\
**Post date:** [February 12, 2015, 12:11pm UTC](https://community.unix.com/t/bash-while-loop-behavior/350367/1 "2015-02-12T12:11:29Z")

</div>

Good Morning

I think there may be something I dont understand fully.

The following code works well, but I dont like the set domen method.

```nohighlight
#!/bin/bash
#
domen="y"
while ["$domen" == "y"]
do
echo " M A I N - M E N U"
echo "1. Contents of /etc/passwd"
echo "2. List of users currently logged"
echo "3. Prsent handling directory"
echo "4. Exit"
echo "Please enter option [1-4] : "
read theopt
echo $theopt
case $theopt in
1) echo $opt;;
2) echo $opt;;
3) echo $opt;;
4) exit 0;;
*) echo "Bad opt";;
esac
done

```

So I looked in one of my books (Written by Chris FA Johnson) and on the web for a second example. The following doesnt work. You'll have to run it to see what I mean. Ill try to explain. As I run through the menu options to test, at one point or another, my option isnt read and the behavior is kind of like a back space. Even when using -n1 for read behavior was odd.

```nohighlight
#!/bin/bash
#
while : 
do
echo " M A I N - M E N U"
echo "1. Contents of /etc/passwd"
echo "2. List of users currently logged"
echo "3. Prsent handling directory"
echo "4. Exit"
echo "Please enter option [1-4] : "
read theopt
echo $theopt
case $theopt in
1) echo $opt;;
2) echo $opt;;
3) echo $opt;;
4) exit 0;;
*) echo "Bad opt";;
esac
done

```

Chris' example does provide a function to do the reading of the options, but I want to understand whats going on here.

As usual thanks for the help !  
Pop

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [February 12, 2015, 12:22pm UTC](https://community.unix.com/t/bash-while-loop-behavior/350367/2 "2015-02-12T12:22:44Z")

</div>

In what way does it not work?

```nohighlight
$ ./oneopt.sh
 M A I N - M E N U
1. Contents of /etc/passwd
2. List of users currently logged
3. Prsent handling directory
4. Exit
Please enter option [1-4] :
1
1

 M A I N - M E N U
1. Contents of /etc/passwd
2. List of users currently logged
3. Prsent handling directory
4. Exit
Please enter option [1-4] :
2
2

 M A I N - M E N U
1. Contents of /etc/passwd
2. List of users currently logged
3. Prsent handling directory
4. Exit
Please enter option [1-4] :
3
3

 M A I N - M E N U
1. Contents of /etc/passwd
2. List of users currently logged
3. Prsent handling directory
4. Exit
Please enter option [1-4] :
4
4

$

```

I see nothing wrong.

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/5_5575768a8748004e209b776fc1b2916d.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [February 12, 2015, 12:25pm UTC](https://community.unix.com/t/bash-while-loop-behavior/350367/3 "2015-02-12T12:25:52Z")

</div>

I can't replicate the error that you report. Did you consider `bash` 's `select` builtin?

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [February 12, 2015, 12:35pm UTC](https://community.unix.com/t/bash-while-loop-behavior/350367/4 "2015-02-12T12:35:29Z")

</div>

I just realized something. By 'backspace' do you mean 'goes to the beginning of the line'? Your file may have carriage returns in it. What have you been editing it with?

---

<div class="post-metadata">

**Author:** ![DGPickett](https://community.unix.com/letter_avatar/dgpickett/32/5_5575768a8748004e209b776fc1b2916d.png) [@DGPickett](https://community.unix.com/u/DGPickett)\
**Post date:** [February 12, 2015, 12:44pm UTC](https://community.unix.com/t/bash-while-loop-behavior/350367/5 "2015-02-12T12:44:39Z")

</div>

I'd "double quote" the $variables so the script does not fail for all whitespace in or shell sensitive characters.

You read 'theopt' but echo 'opt', which is never set.

Usually ':' prompts have no linefeed, like: 'echo "Please enter option [1-4] : \c"' or use the 'read -p prompt' option.

---

<div class="post-metadata">

**Author:** ![popeye](https://community.unix.com/user_avatar/community.unix.com/popeye/32/884_2.png) [@popeye](https://community.unix.com/u/popeye)\
**Post date:** [February 12, 2015, 1:13pm UTC](https://community.unix.com/t/bash-while-loop-behavior/350367/6 "2015-02-12T13:13:11Z")

</div>

VERY INTERESTING !

Ive tried this on a VM at school, a Ubuntu VM on my latptop and a Centos VM on my laptop.

What happens on all is that as I run through and test each option, at one point it seems that the read doesnt except input. It really behave like a backspace, but what I suspect is happening is that read is executing again. Kind of ignores input.

PS Stumped the instructor too !!!

---------- Post updated at 01:13 PM ---------- Previous update was at 01:05 PM ----------

I just tried to execute it on a work VM ... shhh dont tell the boss. Its running on that vserver. What the heck ? Only difference is that Im using VMPLAYER on my laptop and at school. .... scratching my head. Technical problem between chair and keyboard ??

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [February 12, 2015, 1:19pm UTC](https://community.unix.com/t/bash-while-loop-behavior/350367/7 "2015-02-12T13:19:04Z")

</div>

We will not know exactly what you mean unless you copy-paste the output of it misbehaving.

Also, from earlier:

> [@corona688](#):
>
> Your file may have carriage returns in it. What have you been editing it with?

---

<div class="post-metadata">

**Author:** ![popeye](https://community.unix.com/user_avatar/community.unix.com/popeye/32/884_2.png) [@popeye](https://community.unix.com/u/popeye)\
**Post date:** [February 12, 2015, 1:23pm UTC](https://community.unix.com/t/bash-while-loop-behavior/350367/8 "2015-02-12T13:23:46Z")

</div>

Im editing with vi. Ive :set list and dont see any extraneous items in the script files on the VMPLAYER VMs. I will also double quoted all the vars when I get to the case. Yep. At the time I was looking at this I didnt really care what the case was doing because it was a read problem I was having.

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [February 12, 2015, 1:26pm UTC](https://community.unix.com/t/bash-while-loop-behavior/350367/9 "2015-02-12T13:26:35Z")

</div>

Could you copy-paste the output of it misbehaving? I really don't understand what "like a backspace" means.

You could also do

```nohighlight
$ script 
Script started, file is typescript
$ ./myscript.sh

...

$ exit

```

and attach the typescript file it creates to the forum so we can see what's happening.

---

<div class="post-metadata">

**Author:** ![popeye](https://community.unix.com/user_avatar/community.unix.com/popeye/32/884_2.png) [@popeye](https://community.unix.com/u/popeye)\
**Post date:** [February 12, 2015, 1:43pm UTC](https://community.unix.com/t/bash-while-loop-behavior/350367/10 "2015-02-12T13:43:21Z")

</div>

DOPEY !!!! I stripped down everything that wasnt really needed for the forum and posted.

I grabbed the script lines I posted on the forum and ran that on the work VM. Worked fine but still failing on my laptop.

Brought up works VM and my laptop VM. Eyeballed line by line.  
... sitting here thinking and laughing at myself "What a freekin idiot !"

Put a clear between the do and echo Main ... and you'll see what I was talking about. At least I realized it was probably me !!!

🙂 sheez !!

Thanks guys !

---

<div class="post-metadata">

**Author:** ![sea](https://community.unix.com/user_avatar/community.unix.com/sea/32/2946_2.png) [@sea](https://community.unix.com/u/sea)\
**Post date:** [February 12, 2015, 2:25pm UTC](https://community.unix.com/t/bash-while-loop-behavior/350367/11 "2015-02-12T14:25:02Z")

</div>

I just wonder what you expected output is from `echo $opt` as `opt` is never set, only `theopt` .

hth
