String contains in bash

String contains in bash

Using bash, I have a string:

string=`echo My string`

How can I test if it contains another string?

if [ $string ?? 'foo' ] then;
  echo "It's there!";
fi;

Where ?? is my unknown operator. Do I use echo and grep?

if [ `echo $string || grep 'foo' ` ] then;
  echo "It's there!";
fi;

That looks a bit clumsy.

Bash: String contains hyphen

I am trying to see if string1 contains another string2. I do this in this manner: a=$(tempfile) echo eafg > $a if [[ $a == *e* ]] then echo contains fi Now I try to see if a string contains a

Bash: check if string contains characters in regex pattern

How do I check if a variable contains characters (regex) other than 0-9a-z and – in pure bash? I need a conditional check. If the string contains characters other than the accepted characters above si

bash – check if string contains newline

What is the simplest way to check if a string contains newline? For example, after FILE=$(find . -name pattern_*.sh) I’d like to check for newline to ensure only one file matched.

How to check if a file contains a specific string using bash

I want to check if a file contains a specific string or not in bash. I used this script, but it doesn’t work: if [[ ‘grep ‘SomeString’ $File’ ]];then Some Actions fi what’s wrong in my code?

Use bash to find a folder name that contains a string

I would like to do this in Bash: in the current directory, find the first folder that contains foo in the name I’ve been playing around with the find command, but a little confused. Any suggestion

Executing a string as a command in bash that contains pipes

I’m trying to list some ftp directories. I can’t work out how to make bash execute a command that contains pipes correctly. Here’s my script: #/bin/sh declare -a dirs=(/dir1 /dir2) # … and lots

jQuery String Contains Manipulation?

In most languages like C# for example given a string you can test (boolean) if that string contains another string, basically a subset of that string. string x = test2; if(x.contains(test)) // do so

.NET string contains string[]

I’m having a small issue putting together this contains statement any help would be awesome. string betaFilePath = @C:/resultsalpha.txt; StringBuilder sb = new StringBuilder(); using (FileStream fs

string array.Contains?

.NET 2 string[] myStrings = GetMyStrings(); string test = testValue; How can I verify if myStrings contains or not test?

how to check if a word contains all letters in a string bash

let’s say I have a file containing words (one per line), and I have a string containing letters str = aeiou I want to check how many words in the file contain all the letters in string. They don’t

Answers

I am not sure about using an if statement, but you can get a similar effect with a case statement:

case "$string" in 
  *foo*)
    # Do stuff
    ;;
esac

You can use Marcus’s answer (* wildcards) outside a case statement, too, if you use double brackets:

string='My string';

if [[ $string == *My* ]]
then
  echo "It's there!";
fi

The accepted answer is best, but since there’s more than one way to do it, here’s another solution:

if [ "$string" != "${string/foo/}" ]; then
    echo "It's there!"
fi

${var/search/replace} is $var with the first instance of search replaced by replace, if it is found (it doesn’t change $var). If you try to replace foo by nothing, and the string has changed, then obviously foo was found.

If you prefer the regex approach:

string='My string';

if [[ $string =~ .*My.* ]]
then
   echo "It's there!"
fi

I’d use grep, and not the [ command.

Just do:

if grep -q foo <<<$string; then
    echo "It's there"
fi

The -q option makes grep not output anything, as we only want the return code. <<< makes the shell expand the next word and use it as the input to the command, a one-line version of the << here document (I’m not sure whether this is standard or a bashism).

grep -q is useful for this purpose.

The same using awk:

$ string="unix-bash 2389"
$ character="@"

$ printf '%s' "$string" | awk -vc="$character" '{if(gsub(c,"")) print "Found";else print "Not Found"}'

Output:

Not Found
$ character="-"

$ printf '%s' "$string" | awk -vc="$character" '{if(gsub(c,"")) print "Found";else print "Not Found"}'

Output:

Found

Original source: http://unstableme.blogspot.com/2008/06/bash-search-letter-in-string-awk.html

How about this:

text="   <tag>bmnmn</tag>  "
if [[ "$text" =~ "<tag>" ]]; then
   echo "matched"
else
   echo "not matched"
fi

Regards, Stefan

Try oobash it is an OO-style string library for bash 4. It has support for German umlauts. It is written in bash. Many functions are available: -base64Decode, -base64Encode, -capitalize, -center, -charAt, -concat, -contains, -count, -endsWith, -equals, -equalsIgnoreCase, -reverse, -hashCode, -indexOf, -isAlnum, -isAlpha, -isAscii, -isDigit, -isEmpty, -isHexDigit, -isLowerCase, -isSpace, -isPrintable, -isUpperCase, -isVisible, -lastIndexOf, -length, -matches, -replaceAll, -replaceFirst, -startsWith, -substring, -swapCase, -toLowerCase, -toString, -toUpperCase, -trim, and -zfill.

Look at the contains example:

[Desktop]$ String a testXccc                                                  
[Desktop]$ a.contains tX                   
true                                                           
[Desktop]$ a.contains XtX      
false      

oobash is available at Sourceforge.net.

here comes one:

[ $(expr $mystring : ".*${search}.*") -ne 0 ] && echo 'yes' ||  echo 'no' 

I found to need this functionality quite frequently, so I’m using a home-made shell function in my .bashrc like this which allows me to re-use it as often as I need to, with an easy to remember name:

function stringinstring()
{
    case "$2" in 
       *"$1"*)
          return 0
       ;;
    esac   
    return 1
}

To test if $string1 (say, abc) is contained in $string2 (say, 123abcABC) I just need to run stringinstring “$string1” “$string2” and check for the return value, for example

stringinstring "$str1" "$str2"  &&  echo YES  ||  echo NO

This also works:

if echo "$haystack" | egrep -q "$needle" ; then
  echo "Found needle in haystack"
fi

And the negative test is:

if ! echo "$haystack" | egrep -q "$needle" ; then
  echo "Did not find needle in haystack"
fi

I suppose this style is a bit more classic — less dependent upon features of Bash shell.

Note: In a tight loop this code will be much slower than using internal Bash shell features, as one (or two) separate processes will be created and connected via pipes.

this answer was the only one to trap space and dash chars

# for null cmd args checking   
export to_check=' -t'
export space_n_dash_chars=' -'
[[ $to_check == *"$space_n_dash_chars"* ]] && echo found

Compatible answer

As there is already a lot of answer using bashism, there is a way working under poor shell, like busybox:

[ -z "${string##*$reqsubstr*}" ]

In practice, this could give:

string='echo "My string"'
for reqsubstr in 'o "M' 'alt' 'str';do
  if [ -z "${string##*$reqsubstr*}" ] ;then
      echo "String '$string' contain substring: '$reqsubstr'."
    else
      echo "String '$string' don't contain substring: '$reqsubstr'."
    fi
  done

This was tested under bash, dash, ksh and ash (busybox), result is always:

String 'echo "My string"' contain substring: 'o "M'.
String 'echo "My string"' don't contain substring: 'alt'.
String 'echo "My string"' contain substring: 'str'.

Into one function

Ok, as asked by @EeroAaltonen there is a version of same demo, tested under same shells:

myfunc() {
    reqsubstr="$1"
    shift
    string="[email protected]"
    if [ -z "${string##*$reqsubstr*}" ] ;then
        echo "String '$string' contain substring: '$reqsubstr'.";
      else
        echo "String '$string' don't contain substring: '$reqsubstr'." 
    fi
}

Then:

$ myfunc 'o "M' 'echo "My String"'
String 'echo "My String"' contain substring 'o "M'.

$ myfunc 'alt' 'echo "My String"'
String 'echo "My String"' don't contain substring 'alt'.

Notice: you have to escape or double enclose quotes and/or double quotes:

$ myfunc 'o "M' echo "My String"
String 'echo My String' don't contain substring: 'o "M'.

$ myfunc 'o "M' echo /"My String/"
String 'echo "My String"' contain substring: 'o "M'.

Simple (sexy) function

This was tested under busybox, dash and, of course bash:

stringContain() { [ -z "${2##*$1*}" ]; }

That’s all folks!

Than now:

$ if stringContain 'o "M3' 'echo "My String"';then echo yes;else echo no;fi
no
$ if stringContain 'o "M' 'echo "My String"';then echo yes;else echo no;fi
yes

So there are lots of useful solutions to the question – but which is fastest / uses the least resource?

Repeated tests using this frame:

/usr/bin/time bash -c 'a=two;b=onetwothree; x=100000; while [ $x -gt 0 ]; do TEST ; x=$(($x-1)); done'

Replacing TEST each time:

[[ $b =~ $a ]]           2.92user 0.06system 0:02.99elapsed 99%CPU

[ "${b/$a//}" = "$b" ]   3.16user 0.07system 0:03.25elapsed 99%CPU

[[ $b == *$a* ]]         1.85user 0.04system 0:01.90elapsed 99%CPU

case $b in *$a):;;esac   1.80user 0.02system 0:01.83elapsed 99%CPU

doContain $a $b          4.27user 0.11system 0:04.41elapsed 99%CPU

(doContain was in F. Houri’s answer)

And for giggles:

echo $b|grep -q $a       12.68user 30.86system 3:42.40elapsed 19%CPU !ouch!

So the simple substituion option predicatbly wins whether in an extended test or a case. The case is portable.

Piping out to 100000 greps is predictably painful! The old rule about using external utilities without need holds true.

As Paul mentioned in his performance comparison:

if echo "abcdefg" | grep -q "bcdef"; then
    echo "String contains is true."
else
    echo "String contains is not true."
fi

This is POSIX compliant like the ‘case “$string” in’ answer provided by Marcus, but is slightly easier to read than the case statement answer. Also note that this will be much much slower than using a case statement, as Paul pointed out, don’t use it in a loop.

[[ $string == *foo* ]] && echo "It's there" || echo "Couldn't find"

I like sed.

substr="foo"
nonsub="$(echo "$string" | sed "s/$substr//")"
hassub=0 ; [ "$string" != "$nonsub" ] && hassub=1

Edit, Logic:

  • Use sed to remove instance of substring from string

  • If new string differs from old string, substring exists

Exact word match:

string='My long string'
exactSearch='long'

if grep -E -q "/b${exactSearch}/b" <<<${string} >/dev/null 2>&1
  then
    echo "It's there"
  fi

My .bash_profile and how I used grep if the PATH included my 2 bin dirs, don’t append them

# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

U=~/.local.bin:~/bin

if ! echo "$PATH" | grep -q "home"; then
    export PATH=$PATH:${U}   
fi