Skip to main content
deleted 188 characters in body
Source Link
Joseph R.
  • 40.5k
  • 8
  • 113
  • 146

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 white spaceswhitespace. This can be remedied by something like:

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

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

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

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 white spaces. This can be remedied by something like:

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

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 $.

deleted 188 characters in body
Source Link
Joseph R.
  • 40.5k
  • 8
  • 113
  • 146

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

Note that this may produce a false positive in some cases (see terdon's comment)One caveat with the file approach:

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

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

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" =~ $(paste -sd'|' /your/file) ]]; then
    echo "$WORD is in the list"
else
    echo "$WORD is not in the list"
fi

Note that this may produce a false positive in some cases (see terdon's comment)

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 white spaces. This can be remedied by something like:

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

added 244 characters in body
Source Link
Joseph R.
  • 40.5k
  • 8
  • 113
  • 146

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" =~ $(paste -sd'|' /your/file) ]]; then
    echo "$WORD is in the list"
else
    echo "$WORD is not in the list"
fi

Note that this may produce a false positive in some cases (see terdon's comment)

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" =~ $(paste -sd'|' /your/file) ]]; then
    echo "$WORD is in the list"
else
    echo "$WORD is not in the list"
fi

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" =~ $(paste -sd'|' /your/file) ]]; then
    echo "$WORD is in the list"
else
    echo "$WORD is not in the list"
fi

Note that this may produce a false positive in some cases (see terdon's comment)

added 244 characters in body
Source Link
Joseph R.
  • 40.5k
  • 8
  • 113
  • 146
Loading
Source Link
Joseph R.
  • 40.5k
  • 8
  • 113
  • 146
Loading