select is a special, pre-defined kind of loop, designed to do one simple job; create a simple menu out of the list of items you give it. For more flexibility, the more general while true loop is needed, as you've discovered.
In both cases, you usually use the
break and
continue keywords to control the function of the loop.
exit, OTOH, leaves the script entirely.
You only need to set PS3 with
select, because it uses it for its prompt. For the
while loop, just
echo the line directly. You can also use
read's
-p option.
Well done using a function, by the way. Most new scripters don't get that fancy.
PS: Instead of listing out a whole a bunch of numbers by hand, you can use
brace expansion.
Code:
select i in {1..20}