Open In App

Shell Scripting – Select Loop

Improve
Improve
Like Article
Like
Save
Share
Report

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:


Last Updated : 04 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads