0

as above.

basically I want to implement something like

if not match then
  do these things
else
  do these other things
fi

Thanks

1
  • 1
    Why would you not just swap the do these blocks ? Feb 5 at 9:44

2 Answers 2

3

It depends on what you mean by match, but if you mean "exactly match", you can use the string match builtin with a plain argument.

if not string match --quiet -- "some_string" $some_argument
    echo no match
else
    echo match
end

To match within a string, you can use a glob in some_string, or a regular expression with string match --regex.

2

As an alternative to not string match -q -- $pattern $string (as mentioned by @Zanchey), you can also do:

if string match -vq -- $pattern $string
  echo no match
else
  echo match
end

(-v like in grep, or --invert (like GNU grep's --invert-match) to invert the match).

You'll see a difference when matching the pattern against more than one string (like when $string above is a list with more than one element) or when matching against no string at all ($string is an empty list).

if not string match -q -- $pattern $string1 $string2
  echo none of the strings matched
else
  echo at least one the strings matched
end
if string match -vq -- $pattern $string1 $string2
  echo at least one of the strings did not match
else
  echo all the strings matched
end
1
  • I tried something like this but I used "-q -v" and it didn't for me. just tried it again and it worked. thanks
    – mrjayviper
    Feb 7 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.