Skip to content

Instantly share code, notes, and snippets.

@sinecode
Last active August 17, 2020 15:28
Show Gist options
  • Save sinecode/6e511f34da96255229a67c48b6a129c4 to your computer and use it in GitHub Desktop.
Save sinecode/6e511f34da96255229a67c48b6a129c4 to your computer and use it in GitHub Desktop.
Check if an element is in a list in POSIX shell
# Given a list represented as "element1 element2 element3 ..."
# Given an element
# This is a function that returns "yes" or "no" whether the list contains the string
contains() {
# $1 is the list
# $2 is the element to look for
if echo "$1" | grep -q "$2"; then
echo "yes"
else
echo "no"
fi
}
# Examples:
#
# contains "hello everyone" "hello" -> return "yes"
#
# contains "hello everyone" "goodbye" -> return "no"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment