Last active
August 17, 2020 15:28
-
-
Save sinecode/6e511f34da96255229a67c48b6a129c4 to your computer and use it in GitHub Desktop.
Check if an element is in a list in POSIX shell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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