23

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.

Armageddon
  • 371
  • 1
  • 2
  • 7

2 Answers2

16

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 ;-) ).

DevSolar
  • 67,862
  • 21
  • 134
  • 209
2

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.

PradyJord
  • 2,136
  • 12
  • 19
  • 2
    Hm... calling an external process is hardly an improvement, neither performance- nor readability-wise. – DevSolar May 26 '14 at 13:40