115

I have a string ${testmystring} in my .sh script and I want to check if this string does not contain another string.

    if [[ ${testmystring} doesNotContain *"c0"* ]];then
        # testmystring does not contain c0
    fi 

How can I do that, i.e. what is doesNotContain supposed to be?

3
  • 1
    [[ $testmystring != *c0* ]] && echo testmystring does not contain c0 Commented May 31, 2015 at 14:06
  • 1
    possible duplicate of String contains in bash Commented May 31, 2015 at 14:18
  • It's a inversed duplicate: read this answer and use else or not like in: if ! stringContain "c0" "$myteststring" ;then .... Commented May 31, 2015 at 14:23

3 Answers 3

188

Use !=.

if [[ ${testmystring} != *"c0"* ]];then
    # testmystring does not contain c0
fi

See help [[ for more information.

Sign up to request clarification or add additional context in comments.

13 Comments

The { } and the quotes around c0 are superfluous.
I agree they're superfluous for the mere example shown above. But if the variable name and the pattern string are getting more complex (e.g.: containing space in the pattern string), quoting them is necessary. Anyway, quoting them usually does no harm. :)
True it does no harm, and if you enjoy typing them then knock yourself out! Sorry, I know this is pedantic, its just that I find people often use things like { } by rote without understanding when they are needed and when they are not (I did upvote you).
@cdarke: as far as i know not using {} incurs in a performance penalty because the shell cannot immediately assume it is a variable while with it, it can. I am not completely absolutely sure and worse i cant remember where I saw the numbers.
@Randyman99: while I agree with your aim, I don't believe that adding superfluous characters when you don't understand what they do develops good programming practice. That's my point - too many people use them because of a cargo culture and they never learn what they are actually for - that's not good programming practice. Defensive programming is good, mindless programming is not good. Take that from someone who has been programming professionally for fifty years.
|
26

Bash allow u to use =~ to test if the substring is contained. Ergo, the use of negate will allow to test the opposite.

fullstring="123asdf123"
substringA=asdf
substringB=gdsaf
# test for contains asdf, gdsaf and for NOT CONTAINS gdsaf 
[[ $fullstring =~ $substring ]] && echo "found substring $substring in $fullstring"
[[ $fullstring =~ $substringB ]] && echo "found substring $substringB in $fullstring" || echo "failed to find"
[[ ! $fullstring =~ $substringB ]] && echo "did not find substring $substringB in $fullstring"

1 Comment

I think [[ ! $fullstring =~ $substringB ]] is the most fitting answer to this question. thanks
8

As mainframer said, you can use grep, but i would use exit status for testing, try this:

#!/bin/bash
# Test if anotherstring is contained in teststring
teststring="put you string here"
anotherstring="string"

echo ${teststring} | grep --quiet "${anotherstring}"
# Exit status 0 means anotherstring was found
# Exit status 1 means anotherstring was not found

if [ $? = 1 ]
then
  echo "$anotherstring was not found"
fi

2 Comments

You are using a cannon to kill a mosquito
Jahid is probably right, but, if someone is bent on this approach, this oneliner is more compact: if echo $teststring | grep -q $anotherstring ; then echo "found"; else echo "not found"; fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.