Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Not Equal Tilde in bash

Like presented in the title, What's the opposite of "=~" operator? I really googled for that and I fund just the following solution: [[ ! $str1 =~ $str2 ]] Is there any operator that verify the opposite of "=~" operator?

Thanks.

like image 670
Armageddon Avatar asked May 26 '14 11:05

Armageddon


2 Answers

There is no opposing operator in bash up to version 4.3 (current at the time of this post).

[[ ! str1 =~ str2 ]] is the way to go.

For such questions, you should use man instead of your favourite search engine. The man page of the tool involved -- bash, in this case -- is authoritative, the 'web is hearsay (unless, of course, it led you to the man page ;-) ).

like image 161
DevSolar Avatar answered Nov 16 '22 21:11

DevSolar


Answer is There is no such Operator in bash Which does !~ as its there in perl, if you do not want to use already known ( [[ ! $str1 =~ $str2 ]] ) for the purpose then you shall have to use grep or perl . Something like:

x="I like negation"
y="like"


if [[ $(perl -ne "!/$y/ && print" <(echo $x)) == "$x" ]]; then 
echo "Contains"
else
echo "Doesn't contain"

But I don't find any reason to miss !~ in bash as [[ ! $str1 =~ $str2 ]] solves the purpose pretty well. May be that the reason its not there in bash.

Consider the first comment on this Answer from DevSolar, It holds when you will go with grep or perl, which again makes [[ ! $str1 =~ $str2 ]] the best out of available.

like image 41
PradyJord Avatar answered Nov 16 '22 21:11

PradyJord