Unable to match any option in bash select expression

I am trying to use the bash operator selectfor a command loop. The variable in the select clause is always empty. Here is a simple script that illustrates the problem:

#!/bin/bash

select term in one two exit
do
  echo you selected $term
  case $term in
    one ) echo one; break;;
    two ) echo two; break;;
    exit ) echo will exit; return;;
  esac
done

Here is what happens when I run this script:

$ ./test.sh 
1) one
2) two
3) exit
#? one
you selected
#? two
you selected
#? exit
you selected
#? ^D

Does anyone know what I can do wrong? I am on Mac OS X 10.7.3. /bin/bash --versionshows: GNU bash, version 3.2.48 (1) -release (x86_64-apple-darwin11)

+3
source share
2 answers

The script works if you enter "1" or "2" and not "one" or "two".

+5
source

@jedwards . , , , -

select term in first second exit; do
  [[ -z $term ]] && casevar=$REPLY || casevar=$term. # or, shorter, casevar=${term:-$REPLY}
  case $casevar in
    1|first) echo "the first option"; break ;;
    2|second) echo "option no. 2"; break ;;
    3|exit) echo bye; break ;;
  esac
done

bash:

. , , . , . EOF , . , null. REPLY.

+6

All Articles