Skip to content

test: Expected a combining operator like '-a' at index 2 encountered when using test with -o to combine two string comparisons #5186

Closed

Description

@JBlaschke

Using test and -o to check string equivalence with two candidates

set elt "-r"
if test "$elt" = "-r"  -o  "$elt" = "--dependencies"
   echo "hit"
end

results in:

test: Expected a combining operator like '-a' at index 2

System info

fish --version
fish, version 2.7.1
uname -a
Darwin MacBert.local 17.7.0 Darwin Kernel Version 17.7.0: Thu Jun 21 22:53:14 PDT 2018; root:xnu-4570.71.2~1/RELEASE_X86_64 x86_64
echo $TERM
xterm-256color

Activity

  1. zanchey commented on Sep 14, 2018

    @zanchey
    Member

    You've run into a horrible edge-case of the combination of shell variable expansion and the behaviour of test, which other shells suffer from too. The first argument is expanded, so your command looks like this:

    test -r = -r -o -r = --dependencies
    

    As test -r is a valid action, there is no way of test knowing which of the arguments are operators and which are parameters.

    There are two ways to solve this:

  2. JBlaschke commented on Sep 17, 2018

    @JBlaschke
    Author

    Great! thank you... it makes sense now!

  3. quackduck commented on Sep 9, 2020

    @quackduck

    @zanchey Why doesn't if test "$argv[1]" = "--update" throw the same error?

  4. zanchey commented on Sep 9, 2020

    @zanchey
    Member

    --update is not a recognised flag for test.

  5. quackduck commented on Sep 9, 2020

    @quackduck

    Why would that matter?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions