2

Well, even some less-experienced bash users would know meanwhile about the fact that bash arrays would always start with index 0. However, select will always do its own thing, as the following example shows:

#!/bin/bash
choices=(NONE nyc la chicago little_rock miami)

select choice in ${choices[@]}; do
  [[ " ${choices[*]} " == *" $choice "* ]] && break
done
echo "Choice was: $choice ; first element is ${choices[0]}"

Output is:

1) NONE
2) nyc
... etc. ...

BUT ${choices[0]} will output 'NONE' nonetheless! So I feel that somehow there must be a way to force select to start with index 0, not 1.

(Should anyone question the deeper sense of this: there actually is, especially when you have to skip processing frequently (ideally set as '0' in the menu) with more than 9 options and, for convenience's sake, you would put the least-used option as '10' so you may only have to type a single digit in almost every case.)

1

There is no option to change the base of the menu's displayed item number. You mention in your question, "..even some less-experienced bash users would know (about) ... index 0".

Yes, that is correct, but the select menu was not devised for bash users (read "programmers") - It was intended for end-users (pre GUI era) who would typically not know (or care) anything about bash. Being comfortable with a zero based list is a programmer's thing (hence your preference), but it is definitely not something an end-user is used to, at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.