How To

Bash Select Command: How To Create Menus In Shell Scripts

Introduction

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.

What Is The Bash Select Command?

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.

Basic Bash Select Example

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.

Bash Select With Case Statement

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.

Change The Select Prompt

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.

Cybersecurity Example: Simple Security Menu

#!/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.

Conclusion

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

Cyber Defence

Recent Posts

groupdel Command in Linux: Remove a Group and Audit Files

The groupdel command in Linux removes a group from the system. It deletes the group's entry from /etc/group and /etc/gshadow,…

3 hours ago

wc Command in Linux: Count Lines, Words, Characters, and Bytes

The wc command in Linux counts lines, words, characters, and bytes in files or standard input. It…

4 hours ago

top Command in Linux: Monitor Processes and Resource Usage

The top command in Linux provides a real-time view of running processes and system resource usage. From…

4 hours ago

usermod Command in Linux: Modify User Accounts and Groups

The usermod command in Linux modifies existing user account attributes. You can use it to manage group…

4 hours ago

sort Command in Linux: Sort Text, Numbers, Columns, and More

The sort command in Linux reads lines from files or standard input and writes them to standard…

1 day ago

wall Command in Linux: Broadcast Messages to Logged-In Users

The wall command in Linux sends a message to the terminals of all currently logged-in users. The…

1 day ago