Select loop in Shell

The select loop provides an easy way to create a numbered menu from which the user can select. It is useful when you want to ask the user to select one or more items from a list of options.

Syntax in Unix / Linux

 select var in word1 word2 . wordN do cac lenh de thuc thi cho moi word . done 

Here, var is the name of a variable and word1 to wordN is a sequence of characters separated by spaces. Each time the loop executes, the var variable value is set to the next word in the list of words, from word1 to wordN.

For each option, a set of commands will be executed inside the loop. This loop was introduced in ksh and was adjusted into bash . It is not available in sh.

For example in Unix / Linux

The simple example below gives the user a drink option in the list of drinks.

 #!/bin/ksh select DRINK in tea cofee water juice appe all none do case $DRINK in tea | cofee | water | all ) echo "Go to canteen" ;; juice | appe ) echo "Available at home" ;; none ) break ;; *) echo "ERROR: Invalid selection" ;; esac done 

The menu displayed by the select loop looks like this:

 $ ./ test . sh 1 ) tea 2 ) cofee 3 ) water 4 ) juice 5 ) appe 6 ) all 7 ) none #? juice Available at home #? none $ 

You can change the prompt displayed by the select loop by changing the PS3 variable as follows:

 $PS3 = "Please make a selection => " ; export PS3 $ ./ test . sh 1 ) tea 2 ) cofee 3 ) water 4 ) juice 5 ) appe 6 ) all 7 ) none Please make a selection => juice Available at home Please make a selection => none $ 

According to Tutorialspoint

Previous post: Loop until in Shell

Next lesson: Regular Expression in Unix / Linux

4 ★ | 1 Vote

May be interested

  • 11 best tips to get started with Microsoft Loop11 best tips to get started with Microsoft Loop
    microsoft loop is finally available in public preview this month. loop is microsoft's new age collaboration and database solution with a modern and intuitive drag-and-drop editor, similar to notion.
  • The while loop in JavaScriptThe while loop in JavaScript
    while writing a program, you may encounter a situation where you need to perform an iterative action. in these situations, you will need to write loop commands to reduce the number of lines of code.
  • Loop in programming CLoop in programming C
    similar to other programming languages, c also provides us with many control structures and allows you to perform complex parts.
  • How to Fix Boot Loop Problems in WindowsHow to Fix Boot Loop Problems in Windows
    after starting your windows device, you may sometimes experience an infinite boot loop issue (also known as an 'infinite boot loop' error).
  • While loop in PythonWhile loop in Python
    what does the while loop in python do? what is the syntax and how to use while loop? those are the content we will approach in this python lesson.
  • What is Loop mail?What is Loop mail?
    what is loop mail? to help you with this question, the following article will help you find out what loop mail is.
  • For ... loop in JavaScriptFor ... loop in JavaScript
    the for ... in loop is used to iterate over the properties of an object. when we haven't discussed the object yet, you may not feel comfortable with this loop. but once you understand how objects work in javascript, you will find this loop very useful.
  • Loop control in JavaScriptLoop control in JavaScript
    javascript provides high control to handle loop commands and switch commands. there may be a situation when you need to exit the loop without having to go to its endpoint. there may also be situations when you want to jump over a part of the code block and start the next loop.
  • Why Microsoft Loop Can't Compare to NotionWhy Microsoft Loop Can't Compare to Notion
    many people have been using notion for brainstorming for years, so it's interesting to see if loop can replace it as they move deeper into the microsoft ecosystem.
  • What is Google Cloud Shell? What's remarkable about Cloud Shell?What is Google Cloud Shell? What's remarkable about Cloud Shell?
    google cloud shell is an online development and operations environment that you can access anywhere via a web browser. cloud shell is a free service provided by google.