Take the 2-minute tour ×
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.

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
else
    echo no
fi

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

share|improve this question
add comment

7 Answers 7

up vote 10 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"
else
    echo "$WORD is not in the list"
fi

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"
else
    echo "$WORD is not in the list"
fi

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 at 16:27
add comment
case $word in
    dog|cat|horse) echo yes;;
    *)             echo no;;
esac
share|improve this answer
1  
+1 for a portable solution. –  jlliagre Jan 30 at 0:01
add comment

How about:

#!/usr/bin/env bash

WORD="$1"
for w in dog cat horse
do
  if [ "$w" == "$WORD" ] 
  then
      yes=1
      break
  fi
done;
[ "$yes" == "1" ] && echo "$WORD is in the list" || 
                     echo "$WORD is not in the list"

Then:

$ a.sh cat
cat is in the list
$ a.sh bob
bob is not in the list
share|improve this answer
add comment
if (echo "$set"  | fgrep -q "$WORD")
share|improve this answer
1  
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 at 18:25
    
just add -w to grep to avoid partial words –  BrenoZan Jan 29 at 18:34
add comment

You could define a bash function for this:

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

Then use simply like this:

word_valid cat
share|improve this answer
add comment

This worked for me:

#!/bin/bash
phrase=(cat dog parrot cow horse)
findthis=parrot

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

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 at 9:30
add comment

Your Answer

 
discard

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.