How to find substring inside a string (or how to grep a variable)? [duplicate] How to find substring inside a string (or how to grep a variable)? [duplicate] bash bash

How to find substring inside a string (or how to grep a variable)? [duplicate]


LIST="some string with a substring you want to match"SOURCE="substring"if echo "$LIST" | grep -q "$SOURCE"; then  echo "matched";else  echo "no match";fi


You can also compare with wildcards:

if [[ "$LIST" == *"$SOURCE"* ]]


This works in Bash without forking external commands:

function has_substring() {   [[ "$1" != "${2/$1/}" ]]}

Example usage:

name="hello/world"if has_substring "$name" "/"then   echo "Indeed, $name contains a slash!"fi