Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Loop in Unix // Linux: Select Loop in Shell

Understand Loop in Unix // Linux: Select Loop in Shell with clear explanations, practical examples, and useful tips. This updated guide covers the essential...

Table of Contents

Loop in Unix // Linux: Select Loop in Shell is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

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.

As an 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

FAQ

What should you know about 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. As an example, in Unix / Linux?

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

What is Loop in Unix // Linux: Select Loop in Shell?

The select loop provides an easy way to create a numbered menu from which the user can select.

Why is Loop in Unix // Linux: Select Loop in Shell important?

A clear understanding of Loop in Unix // Linux: Select Loop in Shell helps you make informed decisions, avoid common mistakes, and use the relevant tools or techniques more effectively.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.