Sample Header Ad - 728x90

Bash, use case statement to check if the word is in the array

2 votes
3 answers
2517 views
I am writing a script which must accept a word from a limited predefined list as an argument. I also would like it to have completion. I'm storing list in a variable to avoid duplication between complete and case. So I've written this, completion does work, but case statement doesn't. Why? One can't just make case statement parameters out of variables?
declare -ar choices=('foo' 'bar' 'baz')
function do_work {
  case "$1" in 
    "${choices[*]}")
      echo 'yes!'
      ;;
    *)
      echo 'no!'
  esac
}
complete -W "${choices[*]}" do_work
Asked by vatosarmat (252 rep)
Sep 2, 2022, 06:20 AM
Last activity: Sep 3, 2022, 06:14 AM