Search code examples
if-statementfish

Combine if conditions with contains function in fish shell


I can't seem to be able to combine multiple if conditions with contains function, see this example:

> function if_test
      if contains "h" $argv; or contains "-h" $argv
          functions if_test
          return
      end
  end
> if_test h
# ... works fine.
> if_test -h
# ... shows documentation of contains

This is weird because the following functions works as intended, so why is this different?

> if true; and false
     echo what
  else
    echo how
  end
how

> if true; or false
     echo what
  else
    echo how
  end
what

Solution

  • The -h is interpreted as an option to contains - the short form of --help, so it prints contains help. Use -- to signal the end of options:

    if contains h -- $argv; or contains -- -h $argv
    

    Note that the quotes here are merely a placebo - "h" is exactly equivalent to h. If there aren't any special characters (like spaces) quotes don't change anything.