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?
Bash allow u to use =~ to test if the substring is contained. Ergo, the use of negate will allow to test the opposite.
To check if a string contains a substring in Bash, use comparison operator == with the substring surrounded by * wildcards.
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.
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.
Use !=
.
if [[ ${testmystring} != *"c0"* ]];then # testmystring does not contain c0 fi
See help [[
for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With