APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

using bash select

Some material is very old and may be incorrect today

© September 2005 Tony Lawrence

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
 [ $i = "quit" ]  && exit 0
 echo "You chose $i"

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.

You may also want to investigate Dialog or Whiptail.

If you are writing in C, you'll want to use ncurses or Newt.

If you found something useful today, please consider a small donation.

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> using bash select


Inexpensive and informative Apple related e-books:

Take Control of Preview

Take Control of Apple Mail, Third Edition

Take Control of IOS 11

iOS 10: A Take Control Crash Course

Take Control of Automating Your Mac

More Articles by © Tony Lawrence

Mon Sep 11 14:08:08 2006: 2448   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: 2912   ludvigericson

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


Printer Friendly Version

Have you tried Searching this site?

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.

Contact us

Printer Friendly Version

Basic happened to be on a GE timesharing system that was done by Dartmouth, and when GE decided to franchise that, it started spreading Basic around just because it was there, not because it had any intrinsic merits whatsoever. (Alan Kay)

Linux posts

Troubleshooting posts

This post tagged:



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode