Skip to content

test bultin: hyphen in parentheses #1337

Open

Description

@smidm

String with a hyphen in parentheses passed to the test builtin causes an error.

This works:

#!/usr/bin/env fish
set a -h
if test "$a" = "-h"
     echo HIT
end
#!/usr/bin/env fish
set a -h 
if test \( x"$a" = x"-h" \)
     echo HIT
end

This doesn't work:

#!/usr/bin/env fish
set a -h
if test \( "$a" = "-h" \)
     echo HIT
end
#!/usr/bin/env fish
set a -h
if test \( $a = -h \)
     echo HIT
end

It triggers following error:

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

My motivation was the following code:

#!/usr/bin/env fish

if test \( (count $argv) -eq 1 \) -a \( "$argv[1]" = "-h" \)
     echo HIT
end

The only way to get it working was to use an old shell hack and do the comparison like this x"$argv[1]" = x"-h" (see http://www.pixelbeat.org/programming/shell_script_mistakes.html).

Activity

  1. added this to the next-major milestone on Mar 12, 2014
  2. mcandre commented on Aug 15, 2014

    @mcandre

    +1

    I came across this same issue while yak shaving modulinos.

    I'm trying to get if [ "$_" = */scriptedmain ] to match when the script is run in the same directory the script is stored in, as ./scriptedmain. Tried converting the if expression into a case expression, but unlike other shells, fish doesn't allow case wildcards to match dotfiles.

    I changed tactics, tried adding an -o union conditional to the if to match against the dot version more explicitly, but silly fish forgot to add -a/-o to string matching conditionals.

    As a workaround, I'm using an else if instead of -o, but this results in nasty code repetition. A workaround for -a string matching is to nest two if string match blocks.

  3. lilyball commented on Apr 1, 2015

    @lilyball
    Contributor

    This also fails when using boolean logic operators like -a:

    > test foo = foo -a -O = -O
    test: Expected a combining operator like '-a' at index 6
    
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

    bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions