The Bash select command is used to create simple command-line menus in shell scripts. If you are learning Bash scripting, the select command is very useful because it allows users to choose options from a list instead of typing full commands manually.
Menu-based Bash scripts are helpful for Linux automation, system administration, cybersecurity tools, backup scripts, and server management tasks. For example, you can create a menu to check system information, view open ports, scan a target, read logs, or run maintenance commands.
The select command displays a list of options and waits for the user to enter a number. Based on the selected number, the script performs an action.
Basic syntax:
select variable in option1 option2 option3do commanddone
The selected option is stored in the variable.
Create a new Bash script:
nano select-example.sh
Add the following code:
#!/bin/bashselect option in "Show Date" "Show User" "Show Directory" "Exit"do echo "You selected: $option"done
Save and run the script:
chmod +x select-example.sh./select-example.sh
Example output:
1) Show Date2) Show User3) Show Directory4) Exit#?
The user can enter a number to choose an option.
The select command is often used with a case statement to perform different actions.
#!/bin/bashselect option in "Show Date" "Show User" "Show Directory" "Exit"do case $option in "Show Date") date ;; "Show User") whoami ;; "Show Directory") pwd ;; "Exit") echo "Exiting script..." break ;; *) echo "Invalid option" ;; esacdone
This creates a simple interactive menu in the terminal.
By default, Bash shows #? as the prompt. You can change it using the PS3 variable.
#!/bin/bashPS3="Enter your choice: "select tool in "Nmap" "Curl" "Netstat" "Exit"do echo "Selected tool: $tool"done
Now the prompt will look cleaner and more user-friendly.
#!/bin/bashPS3="Choose a security option: "select option in "Show Open Ports" "Show Logged In Users" "Check Failed SSH Logins" "Exit"do case $option in "Show Open Ports") ss -tuln ;; "Show Logged In Users") who ;; "Check Failed SSH Logins") grep "Failed password" /var/log/auth.log | tail -10 ;; "Exit") echo "Goodbye" break ;; *) echo "Invalid choice" ;; esacdone
This script creates a basic cybersecurity menu for checking open ports, logged-in users, and failed SSH login attempts.
The Bash select command is a simple way to create interactive menus in shell scripts. It is easy to use and works well with case statements.
For beginners, learning select helps you build user-friendly Bash scripts. It is useful for Linux automation, system administration, cybersecurity scripts, and command-line tools where users need to choose from multiple options
The groupdel command in Linux removes a group from the system. It deletes the group's entry from /etc/group and /etc/gshadow,…
The wc command in Linux counts lines, words, characters, and bytes in files or standard input. It…
The top command in Linux provides a real-time view of running processes and system resource usage. From…
The usermod command in Linux modifies existing user account attributes. You can use it to manage group…
The sort command in Linux reads lines from files or standard input and writes them to standard…
The wall command in Linux sends a message to the terminals of all currently logged-in users. The…