How to find substring inside a string (or how to grep a variable)?
Solution 1
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
Solution 2
You can also compare with wildcards:
if [[ "$LIST" == *"$SOURCE"* ]]
Solution 3
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
Solution 4
If you're using bash you can just say
if grep -q "$SOURCE" <<< "$LIST" ; then
...
fi
Solution 5
expr match "$LIST" '$SOURCE'
don't work because of this function search $SOURCE from begin of the string and return the position just after pattern $SOURCE if found else 0. So you must write another code:
expr match "$LIST" '.*'"$SOURCE" or expr "$LIST" : '.*'"$SOURCE"
The expression $SOURCE must be double quoted so as a parser may set substitution. Single quoted not substitute and the code above will search textual string $SOURCE from the beginning of the $LIST. If you need the beginning of the string subtract the length $SOURCE e.g ${#SOURCE}. You may write also
expr "$LIST" : ".*\($SOURCE\)"
This function just extract $SOURCE from $LIST and return it. You'll get empty string else. But they problem with double double quote. I don't know how it resolve without using additional variable. It's light solution. So you may write in C. There is ready function strstr. Don't use expr index, So is very attractive. But index search not substring and only first char.
edumike
Updated on July 09, 2022Comments
-
edumike almost 2 years
I'm using BASH, and I don't know how to find a substring. It keeps failing, I've got a string (should this be an array?)
Below,
LIST
is a string list of database names,SOURCE
is the reply, one of those databases. The following still doesn't work:echo "******************************************************************" echo "* DB2 Offline Backup Script *" echo "******************************************************************" echo "What's the name of of the database you would like to backup?" echo "It will be named one in this list:" echo "" LIST=`db2 list database directory | grep "Database alias" | awk '{print $4}'` echo $LIST echo "" echo "******************************************************************" echo -n ">>> " read -e SOURCE if expr match "$LIST" "$SOURCE"; then echo "match" exit -1 else echo "no match" fi exit -1
I've also tried this but doesn't work:
if [ `expr match "$LIST" '$SOURCE'` ]; then
-
SiegeX over 13 yearsIt's hard to answer any sort of question without knowing what
LIST
andSOURCE
looks like -
SourceSeeker over 13 yearsIn Bash, there's almost always no reason to use
expr
which is an external utility. -
edumike over 13 yearsyes that one solved it: stackoverflow.com/questions/229551/string-contains-in-bash
-
jww almost 6 yearsPossible duplicate of String contains a substring in Bash.
-
-
maganap about 9 yearsYou should always quote the strings, like:
if [[ "$list" == *"$source"* ]]
-
wrlee over 6 yearsThe advantage to this solution is that it doesn't rely on any external commands nor file descriptors (via redirection or pipes).
-
codesniffer about 6 years2 issues to consider with this approach: (1) grep uses regex so some chars will match unexpected or cause errors; (2) Using grep (or anything that's not built in to bash) spawns a separate process which will run slower (only matters in larger scale cases).
-
codesniffer about 6 yearsNote that this is using a regex to search for the substring (and also does an out-of-place replace), so special chars in the search (substring) can cause unexpected results. But if regex is fine, then why not just do a regex search (ie. no need for the replace)? Syntax example: if [[ "$string" =~ $substring ]]
-
Leo over 4 yearsNote that
if [[ *"$SOURCE"* == "$LIST$" ]]
is false. -
Manuel Jordan over 2 yearsI've confirmed that the order is important/mandatory - so
*"$SOURCE"*
must be in the right - otherwise always returns false - why that behavior?