APLawrence - Information and Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds Get APLawrence.com by RSS











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Home > Basics > using bash select
Printer Friendly Version




using bash select



The description of "select" in the bash man page is enough to give anyone a headache.

It almost looks like something a lawyer would write:



  select name [ in word ] ; do list ; done
	 The list of words following in is expanded, generating
	 a list of items.  The set of expanded words is
	 printed  on  the  standard error,  each  preceded
	 by a number.  If the in word is omitted, the positional
	 parameters are printed  (see  PARAMETERS  below).
	 The  PS3 prompt is then displayed and a line read
	 from the stan- dard input.  If the line consists of
	 a number  corresponding  to one  of  the  displayed
	 words, then the value of name is set to that word.
	 If the line is empty, the words and prompt are  dis-
	 played again.  If EOF is read, the command completes.
	 Any other value read causes name to be set to  null.
	 The  line  read  is saved  in  the  variable REPLY.
	 The list is executed after each selection until a
	 break command is executed.  The exit status of select
	 is the exit status of the last command executed in
	 list, or zero if no commands were executed.
 

But it's actually not all that awful. Let's give it a spin:

PS3="Choose: "
select i in a b c quit
 do 
 [ $i = "quit" ]  && exit 0
 echo "You chose $i"
done
 

By gosh, it's a menu, isn't it? You can get much fancier if you want, but just this is useful enough for those quick little jobs.


If this page was useful to you, please click to help others find it:  
Your +1's can help friends, contacts, and others on the web find the best stuff when they search.


2 comments




More Articles by Tony Lawrence - Find me on Google+



Click here to add your comments





Mon Sep 11 14:08:08 2006:   anonymous



In the script above should be write
["$i" = "quit"] instead of [$i = "quit"]. Because choosing elements are
not from {1, 2, 3, 4} list causes the following error message:
"line 6: [: =: unary operator expected".

It happends, because $i is null when choosed elements are not from
{1, 2, 3, 4} list and the command evolves to [ = "quit"].




Fri Mar 9 20:25:24 2007:   ludvigericson


I don't find that cryptic at all. :-)

Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



LOD Communications, Inc.

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide




Buy Kerio from a dealer who knows tech: I sell and support

Kerio Connect Mail server, Control, Workspace and Operator licenses and subscription renewals
pavatar.jpg

This post tagged:

       - Scripting
       - Shell




Unix/Linux Consultants

Skills Tests

Guest Post Here