Search code examples
fish

Test for string equality / string comparison in Fish shell?


How do you compare two strings in Fish (like "abc" == "def" in other languages)?

So far, I've used a combination of contains (turns out that contains "" $a only returns 0 if $a is the empty string, although that hasn't seemed to work for me in all cases) and switch (with a case "what_i_want_to_match" and a case '*'). Neither of these methods seem particularly... correct, though.


Solution

  •   if [ "abc" != "def" ] 
            echo "not equal"
      end
      not equal
    
      if [ "abc" = "def" ]
            echo "equal"
      end
    
      if [ "abc" = "abc" ]
            echo "equal"
      end
      equal
    

    or one liner:

    if [ "abc" = "abc" ]; echo "equal"; end
    equal