TIL: Bash select

TIL: Bash select

Published on 04/18/2018 | 18.04.12018 HE

I was recently researching different bash prompts and stumbled upon an StackExchange answer which explains the different prompt types (PS1, PS2, PS3, and PS4). The answer explains how the PS3 prompt is used for commands like select. I did not know about select before, so that’s the real TIL here!

[…] PS3 is shown when the select command is waiting for input […]

So the PS3 prompt is shown when a bash program is waiting for user input, and it turns out that the select bash command is used for creating a “choice” UI in bash. So what does that mean and how does it work?

Asking interactive questions in Bash

Let’s assume we have a shell script called make-project which bootstraps some project stuff, for example configuration files. We could provide the user with an option to choose a format from a list of available configuration formats like XML or JSON.

We can handle these options via shell arguments like make-project -format xml, but we could also ask questions interactively using select.

The script looks like this.


# Default format

# Ask a question
echo "Which configuration format do you prefer?"
# Create the "UI" with a choice option
select f in xml json; do
  # Break to end the select after something was selected

echo "Format: $format"

The select function takes a variable name (f above) and a list of possible values (xml json) which is seperated by spaces.

select myvariable in value1 value2 value3; do
  # $myvariable will be value1, value2, or value3
  selectedValue = $myvariable

When our little make-project script is run it would looks like this (in Bash 4.4 on Mac OS):

$ ./make-project.sh
Which configuration format do you prefer?
1) xml
2) json
#? _

The last line, #?, is the PS3 prompt and _ is the cursor position. The user can now enter a number (1 for xml or 2 for json) and the selection would be captured and assigned to the format variable in our script. If we add export PS3="Your answer: " to the script it will replace the default PS3 prompt for your script as shown below.

$ ./make-project.sh
Which configuration format do you prefer?
1) xml
2) json
Your answer: _

select is a clean and simple way to create a interactive UI! I tried the code with the zsh and fish shell and it looks the same.



Like this?

This website contains no advertisement, no tracking, no paid articles, and no shady-things-whatsoever. It is just content, provided free of charge and with accessibility in mind.

If you would like to support me in providing content - and code - for free or if this page has helped you or your company, consider supporting me directly.

You can support me via:

A colorful image of me wearing my DIY mask. The image has a colorful glitch effect.

Kevin Gimbel

is a DevOps Engineer and avid (Video) Gamer. He’s also interested in Sci-Fi, Cyberpunk, and dystopian books.

Wearing a mask is a good idea!

You can find out more about me if you wish.