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

Bash Scripting Best Practices Every Beginner Should Know

Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…

2 hours ago

How To Create A Self-Signed SSL Certificate Using Bash And OpenSSL

Introduction A self-signed SSL certificate is a certificate that is created and signed by the…

3 hours ago

How To Debug Bash Scripts Using bash -x And set Commands

Introduction Debugging is an important part of Bash scripting. When a script does not work…

8 hours ago

How To Use Cron Jobs With Bash Scripts For Automation

Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…

9 hours ago

How To Use Pipes In Bash Scripts For Command Chaining

Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…

10 hours ago

How To Use grep, awk, And sed In Bash Scripts

Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…

11 hours ago