The select, while, and until loops
The select
, while
and until
loops are also used to loop and iterate over each item in a list or till the condition is true with slight variations in syntax.
Loop using select
The select loop helps in creating a numbered menu in an easy format from which a user can select one or more options.
The syntax of the select
loop is as follows:
select var in list do # Tasks to perform done
The list
can be pre-generated or specified while using the select
loop in the form [item1 item2 item3 …]
.
For example, consider a simple menu listing the contents of '/
' and asking a user to enter an option for which you want to know whether it is a directory or not:
#!/bin/bash # Filename: select.sh # Description: Giving user choice using ...
Get Linux Shell Scripting Essentials now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.