60

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.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Daisy Leigh Brenecki
  • 7,571
  • 6
  • 28
  • 43
  • 6
    So, turns out `[` is actually a command (`/bin/[` on OS X), **as well as** a Bash builtin, with different syntaxes. Go figure! – Daisy Leigh Brenecki Jun 22 '12 at 10:01
  • this comment just made my day! The [ command is a really powerful tool. – yagooar May 06 '13 at 09:14
  • 7
    Personally, I've actually started using `test` instead of `[` in all my scripts, so that it's clear that it's an external command and not a part of the language. (`test` and `[` are the exact same tool.) Of course, I think `test` is also a Bash builtin. – Daisy Leigh Brenecki May 08 '13 at 06:20
  • 2
    I should probably update this to point out that in Fish 2.x, `test` and `[` are both builtins. However, they have the same syntax as the external `[` command, so the accepted answer is still correct. – Daisy Leigh Brenecki Feb 24 '14 at 06:46

2 Answers2

59
  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
Keith Flower
  • 4,032
  • 25
  • 16
  • 1
    Aha! Odd, I thought I tried square brackets before; maybe it was the single `=` that threw me. – Daisy Leigh Brenecki Jun 22 '12 at 09:46
  • 1
    Yeah, the single '=' throws me as well. – Keith Flower Jun 22 '12 at 16:48
  • 4
    shorter one liner: `[ abc = abc ]; and echo equal" – kzh Jan 07 '15 at 02:47
  • I like it. Coming from bash, I must say fish is doing it right! – smac89 Nov 28 '17 at 19:34
  • 2
    Note that you must have a space between the square brackets and what's inside them. `[` is actually a shortcut to the `test` command, and `]` is an argument to `test` telling it to stop reading args. Without the spaces, `[` isn't interpreted as a command and/or `]` isn't present as an argument. – BallpointBen Jun 01 '18 at 05:38
  • @BallpointBen, Thanks for your reminder. The `space between [` you mentioned drives me crazy. Fortunately you point it out. You saved my day Bro! – LeOn - Han Li Nov 07 '18 at 03:36
22

The manual for test has some helpful info. It's available with man test.

Operators for text strings
   o STRING1 = STRING2 returns true if the strings STRING1 and STRING2 are identical.

   o STRING1 != STRING2 returns true if the strings STRING1 and STRING2 are not
     identical.

   o -n STRING returns true if the length of STRING is non-zero.

   o -z STRING returns true if the length of STRING is zero.

For example

set var foo

test "$var" = "foo" && echo equal

if test "$var" = "foo"
  echo equal
end

You can also use [ and ] instead of test.

Here's how to check for empty strings or undefined variables, which are falsy in fish.

set hello "world"
set empty_string ""
set undefined_var  # Expands to empty string

if [ "$hello" ]
  echo "not empty"  # <== true
else
  echo "empty"
end

if [ "$empty_string" ]
  echo "not empty"
else
  echo "empty"  # <== true
end

if [ "$undefined_var" ]
  echo "not empty"
else
  echo "empty"  # <== true
end
Dennis
  • 56,821
  • 26
  • 143
  • 139