I am trying to use bash to check if any substring in a user input (arg) is in an array of keywords. I can successfully check for the whole string but so far I haven't been able to check for the substring. The code below works for matching the whole string (arg) against members of the keyword_array. Is there a way to tweak my 'if' check so that any string the user inputs that contains "branch" or "switch" will match? I've tried adding *'s in various places and did " ${arg} " =~ " ${keyword_array[*]} " with no luck. I can do this with a for loop but I'm trying to see if there's a way to do it similar to the check that matches the entire string.
keyword_array=("branch" "switch")
arg="switch test" # This won't match
#arg="switch" # This matches
if [[ " ${keyword_array[*]} " =~ " ${arg} " ]] ; then
echo "arg matches"
else
echo "arg does not match"
fi
argis the first element, last element, or a middle element of the array. Even then, the whole point of an array is to store elements that might contain whatever delimiter${...[*]}uses to construct a single string. Just use theforloop. If speed is an issue, you should absolutely not be usingbashin the first place.