Shell Scripting - Select Loop
Last Updated :
04 Jan, 2022
The select loop is one of the categories of loops in bash programming. A select-loop in the shell can be stopped in two cases only if there is a break statement or a keyboard interrupt. The main objective of using a select loop is that it represents different data elements in the form of a numbered list to the user. The user can easily select one of the options as listed by the program.
The syntax of a general select loop is given below,
Syntax:
select myVariable in variable1 variable2 ... variableN
do
# body to be executed for
# every value in the sequence.
done
Here, myVariable is a variable that is used to refer to each of the values from variable1 to variableN.
Example 1:
In the below program we are creating a numbered menu to allow a user (or Btech student) to select the department.
Source Code:
# Program to demonstrate the working of a
# select-loop in shell scripting
# PS3="Enter your choice ==> "
# echo "What is your department?"
select department in CS IT ECE EE
do
case $department in
CS)
echo "I am from CS department."
;;
IT)
echo "I am from IT department."
;;
ECE)
echo "I am from ECE department."
;;
EE)
echo "I am from EE department."
;;
none)
break
;;
*) echo "Invalid selection"
;;
esac
done
Output:
Example 2:
In the below program we are creating a numbered menu to allow a user to select a number. Once a number is selected by the user we are displaying whether the number is even or odd.
Source Code:
# Program to demonstrate the working of
# a select-loop in shell scripting
# PS3="Enter your choice ==> "
# echo "Choose a number:"
select num in 1 2 3 4 5 6 7
do
case $num in
2|4|6|8)
echo "Even number."
;;
1|3|5|7)
echo "Odd number."
;;
none)
break
;;
*) echo "ERROR: Invalid selection"
;;
esac
done
Output:

Select loop with input prompts:
We can prompt users before asking for any selection from the menu with the help of the PS3 variable in bash programming. This variable must be declared before the select loop. The value or string with which a PS3 variable is initialized is used to prompt the user on the console.
Example 1:
In the below program we have prompted the user as "Enter your choice ==>".
Source Code:
# Program to demonstrate the working of a
# select-loop in shell scripting
PS3="Enter your choice ==> "
echo "What is your department?"
select department in CS IT ECE EE
do
case $department in
CS)
echo "I am from CS department."
;;
IT)
echo "I am from IT department."
;;
ECE)
echo "I am from ECE department."
;;
EE)
echo "I am from EE department."
;;
none)
break
;;
*) echo "Invalid selection"
;;
esac
done
Output:
Example 2:
In this program, we have prompted the user as "Enter your choice ==>".
Source Code:
# Program to demonstrate the working of a
# select-loop in shell scripting
PS3="Enter your choice ==> "
echo "Choose a number:"
select num in 1 2 3 4 5 6 7
do
case $num in
2|4|6|8)
echo "Even number."
;;
1|3|5|7)
echo "Odd number."
;;
none)
break
;;
*) echo "ERROR: Invalid selection"
;;
esac
done
Output:

Pipe out options to a select loop in bash
When the end of file (EOF) of input is reached then the select loop gets completed in bash. But in the case of a command piped out to our script the output of the previous command becomes the input for the current command.
Let us understand what does a pipe command means in bash. The pipe is considered one of the most powerful operators in the shell. It is denoted by the symbol (|). The pipe takes the output from one command and uses it as input for another. And, we're not limited to a single piped command but we can stack them as many times as you like, or until you run out of output or file descriptors. But in the case of select-loop using a pipe out command may lead to no output as demonstrated in the below program.
Example:
Source Code:
# Program to demonstrate the working of a
# select-loop in shell scripting
# script: select-loop-bash.sh
select department in CS IT ECE EE
do
case $department in
CS)
echo "I am from CS department."
;;
IT)
echo "I am from IT department."
;;
ECE)
echo "I am from ECE department."
;;
EE)
echo "I am from EE department."
;;
none)
break
;;
*) echo "Invalid selection"
;;
esac
done
Interactive commands (one after the another) and the output:
Piped:

How to fix no output while using the pipe out command?
This issue can be fixed by ensuring that the select menu will be read from the "/dev/tty" and that we are passing the option with proper word delimiter by using either the "echo" or the "printf" commands.
Example:
Source Code:
# Program to demonstrate the working of a
# select-loop in shell scripting
# script: select-loop-bash.sh
select department in CS IT ECE EE
do
case $department in
CS)
echo "I am from CS department."
;;
IT)
echo "I am from IT department."
;;
ECE)
echo "I am from ECE department."
;;
EE)
echo "I am from EE department."
;;
none)
break
;;
*) echo "Invalid selection"
;;
esac
done < /dev/tty
Interactive commands (one after the another) and the output:
Similar Reads
Shell Scripting - Set Command
The `set` command in shell scripting is a powerful tool that used for controlling the behavior of the shell and the environment in which scripts run. It allows the users to modify the shell options and positional parameters which facilitates providing greater control over script execution and debugg
6 min read
Shell Scripting - Subshell
Shell scripting is a powerful tool for automating tasks and simplifying the management of systems and applications. One important concept in shell scripting is the use of subshells, which allow you to execute commands within a separate shell environment. A subshell is a child shell that is spawned b
4 min read
Array Basics Shell Scripting | Set 2 (Using Loops)
It is recommended to go through Array Basics Shell Scripting | Set-1 Introduction Suppose you want to repeat a particular task so many times then it is a better to use loops. Mostly all languages provides the concept of loops. In Bourne Shell there are two types of loops i.e for loop and while loop.
3 min read
Shell Scripting - Shell Variables
A shell variable is a character string in a shell that stores some value. It could be an integer, filename, string, or some shell command itself. Basically, it is a pointer to the actual data stored in memory. We have a few rules that have to be followed while writing variables in the script (which
6 min read
Bash Scripting - While Loop
A while loop is a statement that iterates over a block of code till the condition specified is evaluated to false. We can use this statement or loop in our program when do not know how many times the condition is going to evaluate to true before evaluating to false. Â Table of Content The Syntax of
15+ min read
Bash Scripting - Until Loop
'Bash' provides several looping constructs to control the execution flow in scripts, including 'for', 'while', and 'until' loops. The 'until' loop is a unique looping mechanism that runs a block of code repeatedly until a specified condition becomes true. It essentially works in the opposite manner
7 min read
Shell Scripting - Shopt Command
Unlike many shell commands, shopt is a bash-only shell command. It is a built-in BASH shell command that controls certain options for the shell session. One can consider this as a boolean ON/OFF switch for certain shell options. The âshoptâ command provides control over many settings that are used t
7 min read
Looping Statements | Shell Script
Looping Statements in Shell Scripting: There are total 3 looping statements that can be used in bash programming Table of Content `while` statement in Shell Script in Linux`for` statement in Shell Script in Linux`until` statement in Shell Script in LinuxExamples of Looping StatementsTo alter the fl
10 min read
How to Use Heredoc in Shell Scripting
The Heredoc or Here Document is an input literal used by many programming and scripting languages. The Heredoc was originally used in UNIX Shells and is in fashion till date and, is supported by all popular Linux shells such as zsh, bash, tsh, etc. The symbol for a Heredoc is '<<' two left che
6 min read
Array Basics in Shell Scripting | Set 1
Consider a situation if we want to store 1000 numbers and perform operations on them. If we use a simple variable concept then we have to create 1000 variables and perform operations on them. But it is difficult to handle a large number of variables. So, it is good to store the same type of values i
6 min read