Test for string equality / string comparison in Fish shell?

Leigh Brenecki picture Leigh Brenecki · Jun 19, 2012 · Viewed 20.4k times · Source

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.

Answer

Keith Flower picture Keith Flower · Jun 20, 2012
  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