Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash checking if string does not contain other string

Tags:

bash

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?

like image 811
machinery Avatar asked May 31 '15 12:05

machinery


People also ask

How do you check if a string does not contain a character in bash?

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

How do you check if a string is a substring of another in bash?

To check if a string contains a substring in Bash, use comparison operator == with the substring surrounded by * wildcards.

How do you check if a string is present in another string in shell script?

The grep command can also be used to find strings in another string. In the following example, we are passing the string $STR as an input to grep and checking if the string $SUB is found within the input string. The command will return true or false as appropriate.

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc.


1 Answers

Use !=.

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

See help [[ for more information.

like image 161
cychoi Avatar answered Sep 22 '22 10:09

cychoi