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