What is the `select` statement used for?
BeginnerConceptArrays, conditionals and loops
Answer
select prints a numbered menu from a list and reads the user's choice into a variable, repeating until break.
PS3="Deploy to which environment? "
select env in dev staging prod quit; do
case $env in
quit) exit 0 ;;
"") echo "invalid choice" ;;
*) echo "deploying to $env"; break ;;
esac
done
Useful for interactive runbooks; never in automation, which has no terminal.
More on Arrays, conditionals and loops
How do you declare, populate and iterate an indexed array?BeginnerWhat is the difference between `"${arr[@]}"` and `"${arr[*]}"`?IntermediateHow do associative arrays work and what are they good for in DevOps scripts?IntermediateHow do you read a command's output into an array correctly?IntermediateWhat is the difference between `[ ]`, `[[ ]]` and `(( ))`?BeginnerWhy does `[ $var = "prod" ]` fail when `var` is empty or contains spaces?Intermediate