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
Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…
Introduction A self-signed SSL certificate is a certificate that is created and signed by the…
Introduction Debugging is an important part of Bash scripting. When a script does not work…
Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…
Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…
Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…