Bash script declare -


i trying create simple bash script ask user input , echo value in declares based on number choose.

declare -a bla bla[1]="bla1" bla[2]="bla2"  echo "what bla like?" echo "" echo "1) bla1" echo "2) bla2"  read answer echo "bla[$answer]" 

when run script expecting output either "bla1" or "bla2" depending if typed 1 or 2. although ouput: "bla[1]" or "bla[2]

what doing wrong here?

you want use bash's select command here:

bla=( bla1 bla2 ) ps3="what bla like? " select b in "${bla[@]}";     [[ $b ]] && break done echo "you want: $b" 

notes:

  • a regular array suffices, don't need associative array if you're using integer indices.
  • the array expansion "${bla[@]}" required expand array constituent elements.
  • when user enters valid response, $b variable non-null, , break statement exits select "loop".

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -