Linux offers numerous options, enabling you to customize the execution of your commands. The most common method is parameters. Even something as simple as the command to display the files in a directory – ls – has several parameters that customize its operation. For most scripts and commands, this is the best approach. Sometimes, however, you want a more user-friendly way for the user to select options, and in such situations, the bash select loop is a perfect alternative. It provides an easy way to present users with a menu, allowing them to select the corresponding number and then take actions based on their choice.
Let’s see how the bash select loop works.
Syntax of the Bash Select Loop
As a high-level overview, the bash select loop has the following syntax:
select variable_name in list_of_items
do
# commands to execute based on selection
done
There’s a lot more to fill in, as I’ll show below, but here you can see the basic structure. The “variable_name” stores the value of the chosen item, and the “list_of_items” can be an array, a list of items, or even the output of a command. Within the “do done” loop, we craft the logic of the flow, usually with a “case” command.
The best way to illustrate this would be with an example of a simple bash select loop. For this, I create a file called “menu_example.sh”, and paste the following into it:
#!/bin/bash
# Clear the screen for a cleaner look
clear
echo "Welcome to the Simple Bash Menu!"
echo "---------------------------------"
# Define the options for our menu
options=("View Current Date" "List Files in Current Directory" "Show Disk Usage" "Exit")
# Set the PS3 prompt (optional, but makes it user-friendly)
PS3="Please choose an option (enter the number): "
# Start the select loop
select opt in "${options[@]}"
do
case $opt in
"View Current Date")
echo "---------------------------------"
echo "You chose: View Current Date"
date
echo "---------------------------------"
;;
"List Files in Current Directory")
echo "---------------------------------"
echo "You chose: List Files in Current Directory"
ls -l
echo "---------------------------------"
;;
"Show Disk Usage")
echo "---------------------------------"
echo "You chose: Show Disk Usage"
df -h .
echo "---------------------------------"
;;
"Exit")
echo "---------------------------------"
echo "Exiting the menu. Goodbye!"
echo "---------------------------------"
break # Exit the select loop
;;
*) # Default case for invalid input
echo "---------------------------------"
echo "Invalid option: $REPLY. Please enter a valid number."
echo "---------------------------------"
;;
esac
echo # Add an empty line for better readability between prompts
done
echo "Script finished."
You can read more on how to run .sh files in my earlier tutorial. This generates the following output:

As you can see, I’ve marked my choices in red. First, I chose “1” and the script showed me the current date. Then I chose “2”, which showed me the disk space available on the system. Finally, I exited the menu, and the script printed a final message and let me go.
As you can see, the main logic of the menu is contained in a case statement. It can seem complex, so we have a tutorial that’ll help you understand how to use case statements in bash. But once you have a working example of a case statement, you can just customize it to suit your needs. I won’t go over the exact syntax of the case statement here. All you need to see is that the “opt” variable stores the choice of the user. The list of choices was created earlier in an index array that we just passed to the select loop. Based on the choice of the user, we simply executed the command with some pretty formatting, and then used the “Exit” keyword to come out of the case loop and finish the script.
From within the case statement, you can call other scripts and functions, and if you have a lot of logic, I recommend that you do so to avoid it becoming excessively long and keep the code readable.
Use Cases for the Bash Select Loop
The utility of the bash select loop should be obvious. For one, it allows you to create scripts for regular users who either don’t know or can’t be bothered to type in the commands manually. But it’s also great for processing user choice within scripts. Here are some common use cases for the bash select loop to get you started.
Script Configuration
Beyond making it easy for users to select the options of a command, the bash select loop lets you fork your script into different directions. For example, you might ask your user to select a directory or a file. For example, you might want to write a script allowing the user to pick a certain log file to view. Log files are notoriously hard to find in Linux, and you might want to do your users’ work for them by providing them with a list of log files from which they can choose.
You can populate the “select” options dynamically by manually selecting the log files, or by using an “ls” statement. For example:
select file in $(ls *.log); do … done
Of course, you can craft the “ls” statement to obtain the files from the location that you want.
System Administration Streamlining
You can provide a single script for users on your system to perform basic administrative actions like restarting the web server, checking disk usage, or viewing system logs. This way, you can control the kinds of actions your users can take.
For lay users, particularly, it can be hard to remember the exact parameters to use, so you can code them directly into the script.
Script Coordination
The bash select loop can help you craft your workflows such that you write a main “control script” that’s the same for everyone, and then you can branch out to other scripts depending on the requirements. I have recurring requirements that I repeatedly perform. For example, you can have a script that lets the user choose to restart Docker containers or prune unused images.
Even if you already have scripts to do these things, the select loop can help you coordinate between them, so you only have to run one script.
Guided Workflows
Some operations require multiple steps. For example, while setting up a piece of software, you might want to first install the database, run migrations, deploy code, or more. For newer users, having a bash select script walk them through the process can be extremely helpful, until they’re ready to run the commands on their own.
And in case you thought these are for beginners alone, consider that some cloud resource interactions can be pretty complex. By parsing the output of CLI tools, you can create scripts that present easily selectable options. Using the bash select loop wisely can dramatically speed up routine tasks and put you in a better mood :).
Conclusion
The bash select loop allows you to create easy workflows and even allows your users to make their choices based on the output of specific commands. It’s not just for beginners, either. Experienced admins can streamline their operations without having to copy/paste complex container IDs, and instead have their routine operations depend on just a few numbered presses!

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!

Leave a Reply