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

Best OSINT Tools for Journalists 2026: Verify Sources, Images and Claims

Journalists use OSINT to verify public information before publishing. In 2026, misinformation, AI-generated images, fake…

7 hours ago

Install Docker on Ubuntu 20.04: Complete Step-by-Step Guide

Docker is an open-source platform that lets you package and run applications inside containers. Each container…

17 hours ago

Install PostgreSQL on Ubuntu: Database Setup and Admin Guide

PostgreSQL (often called Postgres) is an open-source relational database system. It supports advanced features like JSON…

18 hours ago

Install Xrdp Remote Desktop on Ubuntu: Setup and Connect

Xrdp is an open-source server that lets you connect to your Ubuntu machine from another computer…

18 hours ago

Tomcat 9 on Ubuntu 20.04: Install, Configure, and Start

Apache Tomcat is an open-source web server and Java servlet container. It is one of the…

19 hours ago

Automatic Updates on Ubuntu: Set Up unattended-upgrades

Keeping your Ubuntu system updated is one of the best ways to protect it. Security…

20 hours ago