This is a very little-known feature of the Bash language that can be very useful when you have to knock up a script for someone else, particularly when they’re not Unix-savvy enough to be trusted with command line switches – give them a menu interface.
select command is a bash internal (
man bash) and it packs a lot into a single word. What it does is to allow you to create an ennumerated list that functions as an interactive text menu. All that’s required is to set the text of the prompt to the
PS3 variable, and then pass a list of strings to the
select command and a variable name. Whichever option from the list that the user chooses will then be assigned to that variable.
PS3="Choose (1-5):" echo "Choose a name." select name in tom dick harry barbara jerry do break done echo "You chose $name."
Generates the output:
Choose a name. 1) tom 2) dick 3) harry 4) barbara 5) jerry Choose (1-5):1 You chose tom.
The possibilities for creating idiot-proof, condescending, user-operated scripts are endless.
Matt Parsons is a freelance Linux specialist who has designed, built and supported Unix and Linux systems in the finance, telecommunications and media industries.
He lives and works in London.