Skip to content

Linux select command

The Linux select command is a built-in feature of shell scripts that allows users to create simple menus for selecting options. It provides an interactive way to present choices to users, making it easier to navigate through a script. With the select command, users can create custom menus with a list of options displayed on the screen. By choosing a specific option, users can trigger a specific action or command within the script. The select command simplifies the user experience and enhances the interactivity of shell scripts.

select Syntax:

Terminal window
select [options] [parameters]

Options:

OptionDescription
-a, —allSelect all fields
-c, —charactersSpecify characters to select
-t, —linesSelect by line numbers
-f, —fieldsSelect by field number

Parameters:

ParameterDescription
fileFile to select from
patternPattern to match in the selection

select Usage:

Monitor Multiple File Descriptors

Terminal window
select file in /var/log/syslog /var/log/messages /var/log/auth.log
do
tail -f $file
done

This command allows you to monitor multiple file descriptors interactively by using the select loop.

Select from a List of Options

Terminal window
select option in "Option 1" "Option 2" "Option 3" "Exit"
do
case $option in
"Option 1")
echo "Selected Option 1"
;;
"Option 2")
echo "Selected Option 2"
;;
"Option 3")
echo "Selected Option 3"
;;
"Exit")
break
;;
*)
echo "Invalid option"
;;
esac
done

This example demonstrates how to create a menu system using the select command to choose from a list of options.

Monitor System Processes Using Select

Terminal window
select process in "httpd" "sshd" "mysql" "quit"
do
case $process in
"httpd")
ps aux | grep httpd
;;
"sshd")
ps aux | grep sshd
;;
"mysql")
ps aux | grep mysql
;;
"quit")
break
;;
*)
echo "Invalid process"
;;
esac
done

With this command, you can monitor system processes by selecting a specific process to display detailed information using the ps command and grep.

Select from a List of Options and Execute Commands

Terminal window
select option in "Create file" "Delete file" "Exit"
do
case $option in
"Create file")
read -p "Enter file name: " filename
touch $filename
echo "File created: $filename"
;;
"Delete file")
read -p "Enter file name: " filename
rm $filename
echo "File deleted: $filename"
;;
"Exit")
break
;;
*)
echo "Invalid option"
;;
esac
done

In this example, the select command is used to prompt the user to choose between creating a file, deleting a file, or exiting the script, and performs the respective actions based on the selection made.

How do I use select in Linux?

To use the select command in Linux, execute the following command:

Terminal window
select option in option1 option2 option3
do
case $option in
option1)
# Commands to execute for option1
;;
option2)
# Commands to execute for option2
;;
option3)
# Commands to execute for option3
;;
*)
echo "Invalid option selected"
;;
esac
done

What is the purpose of the select command in Linux?

The select command in Linux is used to create a simple menu system for interactive shell scripts. It allows the user to choose from a list of options presented by the script.

How do I handle user input with the select command in Linux?

To handle user input with the select command in Linux, you can use a case statement to execute different commands based on the option selected by the user.

Can I customize the prompt text displayed by the select command in Linux?

Yes, you can customize the prompt text displayed by the select command in Linux by providing a different message before presenting the list of options.

How can I add a default option in the select menu in Linux?

To add a default or exit option in the select menu in Linux, you can include it as one of the choices presented to the user, allowing them to exit the menu if desired.

Is it possible to use select within a function in a shell script?

Yes, you can use the select command within a function in a shell script to create interactive menus that are part of a larger script or program.

How can I loop the select menu in a Linux shell script until a specific condition is met?

You can loop the select menu in a Linux shell script until a specific condition is met by enclosing the select block within a while loop that checks for the desired condition before exiting the loop.

Applications of the select command

  1. Creating a simple menu for user selection in a shell script.
  2. Automating the selection process for a predefined list of options.
  3. Handling user input validation and error prevention in interactive scripts.