Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am looking for a construct in bash, to decide if a variable $WORD is one of defined words. I need something like this:

if "$WORD" in dog cat horse ; then 
    echo yes
    echo no

does bash have such construct? If not, what would be the closest?

share|improve this question
up vote 15 down vote accepted

This is a Bash-only (>= version 3) solution that uses regular expressions:

if [[ "$WORD" =~ ^(cat|dog|horse)$ ]]; then
    echo "$WORD is in the list"
    echo "$WORD is not in the list"

If your word list is long, you can store it in a file (one word per line) and do this:

if [[ "$WORD" =~ $(echo ^\($(paste -sd'|' /your/file)\)$) ]]; then
    echo "$WORD is in the list"
    echo "$WORD is not in the list"

One caveat with the file approach:

  • It will break if the file has whitespace. This can be remedied by something like:

    sed 's/[[:blank:]]//g' /your/file | paste -sd '|' /dev/stdin

Thanks to @terdon for reminding me to properly anchor the pattern with ^ and $.

share|improve this answer
And shopt -s nocasematch might help if you want the search to be case insensitive. – Skippy le Grand Gourou Feb 13 '14 at 16:27
case $word in
    dog|cat|horse) echo yes;;
    *)             echo no;;
share|improve this answer
+1 for a portable solution. – jlliagre Jan 30 '14 at 0:01

How about:

#!/usr/bin/env bash

for w in dog cat horse
  if [ "$w" == "$WORD" ] 
[ "$yes" == "1" ] && echo "$WORD is in the list" || 
                     echo "$WORD is not in the list"


$ a.sh cat
cat is in the list
$ a.sh bob
bob is not in the list
share|improve this answer
if (echo "$set"  | fgrep -q "$WORD")
share|improve this answer
Careful though, this will return true if $WORD is empty, it will match if WORD=ca or WORD=og or similar and I assume you meant echo ${set[@]}. – terdon Jan 29 '14 at 18:25
just add -w to grep to avoid partial words – BrenoZan Jan 29 '14 at 18:34

You could define a bash function for this:

function word_valid() 
  if [ "$1" != "cat" -a "$1" != "dog" -a "$1" != "horse" ];then
    echo no
    echo yes

Then use simply like this:

word_valid cat
share|improve this answer

This worked for me:

phrase=(cat dog parrot cow horse)

for item in ${phrase[*]}
    test "$item" == "$findthis" && { echo "$findthis found!"; break; }
share|improve this answer

You may want to put the list of words into a file, in case you change the list often, or you want it to be shared by multiple scripts.  And you may need to put the words into a file if the list gets too long to manage in a script.  Then you can say

if fgrep –qx "$WORD" word_list
share|improve this answer
no, I don't want to have my list in a file – Martin Vegter Jan 30 '14 at 9:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.