Skip to content

Handling of unmatched file globs should match other shells, return non-zero? #1920

Closed

Description

@geoff-nixon

sh:

$ rm *.foo
rm: *.foo: No such file or directory
$ echo $?
1

$ rm -rf *.foo
$ echo $?
0

$ ls *.bar
ls: *.bar: No such file or directory
$ ls *.bar 2>/dev/null
$ echo $?
1

fish:

~> rm *.foo
No matches for wildcard '*.foo'.
fish: rm *.foo
         ^
usage: rm [-f | -i] [-dPRrvW] file ...
       unlink file
echo $status
1

~> rm -rf *.foo
No matches for wildcard '*.foo'.
fish: rm -rf *.foo
             ^
~> echo $status
0

~> ls *.bar
No matches for wildcard '*.bar'.
fish: ls *.bar
         ^
~> ls *.bar 2>/dev/null
No matches for wildcard '*.bar'.
fish: ls *.bar
         ^
~> echo $status
0

Unless this an intentional deviation?

Activity

  1. zanchey commented on Jan 30, 2015

    @zanchey
    Member

    What's fun is that dash and bash just pass the unexpanded wildcard, which means that echo *.foo > /dev/null returns 0 on those shells.

    zsh sets the return status to 1 if there is no match on the glob, though.

  2. geoff-nixon commented on Jan 30, 2015

    @geoff-nixon
    ContributorAuthor

    Yeesh. Tons o' fun.

    So looking at POSIX man 3 glob and the linked Patterns Used for Filename Expansion, it does seem as though passing the unexpanded glob is the specified behavior:

    If the pattern does not match any existing filenames or pathnames, the pattern string shall be left unchanged.


    To buttress what you're already said:

    • Most other shells (in addition to bash and dash, ksh[93], pdksh, mksh, yash) adhere to the POSIX spec: they pass the unmodified string to the command/function in question, and the output/return value depends on how the command it function handles such a string. That is to say:
      • rm *.foo and cat *.foo return 1, and print [rm|cat]: *.foo: No such file or directory to stderr (quashed with 2>/dev/null)
      • rm -rf *.foo and echo *.foo return 0: rm -rf is silent, and echo *.fooechoes *.foo.
    • It seems that zsh is the only other shell that aggressively errors out on the glob, in which case, it returns non-zero; and always prints zsh: no matches found: *.foo, 2>&1 >/dev/null be damned.
    • Fish has this same basic behavior, but always returns 0.

    I'm not married to the idea that fish should do as the POSIX-like shells do — I mean, no one should be using a fileglob like that in a script in any case (though I see this all the time). But if the current behavior is to be kept, I do thing that 2>/dev/null should quash any error, and it should return 1.

    Neh?

  3. zanchey commented on Feb 2, 2015

    @zanchey
    Member

    Sounds fine to me. The quashing of stderr is covered by #1674. The POSIX behaviour is definitely bad.

  4. added this to the fish-future milestone on Feb 2, 2015
  5. anordal commented on Apr 7, 2015

    @anordal
    Contributor

    Bash's shopt -s nullglob is the sane behaviour. Can't we just drop the error message and return zero arguments?

    I wish fish behaved this way. I think it makes even more sense in fish, where variables are arrays, and empty arrays are easy to test for:

    set foos *.foo
    if test (count $foos) -ge 1
            rm -f $foos
    end
    

    This script fails unnecessarily, because of the dreaded No matches for wildcard '*.foo'

  6. ridiculousfish commented on Apr 7, 2015

    @ridiculousfish
    Member

    The set foos *.foo case is a valid point. But consider a case like ls *.txt. If that glob fails, ls will receive no arguments and so print all files. That's probably not what the user wants.

    Maybe this should vary depending on whether it's run interactively or not?

  7. oconnor663 commented on Jul 24, 2015

    @oconnor663

    @ridiculousfish The fish documentation specifies:

    If none of the wildcarded arguments sent to a command result in any matches, the command will not be executed.

    But it seems at least on version 2.2.0 the command is executed. Is the documentation out of date? I ended up at this issue specifically because I was running ls *.deb, and it was giving me the whole directory instead, and I was very confused :)

  8. oconnor663 commented on Jul 24, 2015

    @oconnor663

    Zsh offers commands like noglob to change the wildcard behavior for whatever follows. Maybe fish could go with the most conservative behavior, but offer commands to loosen things up as needed, something like:

    ls *.deb            # prints error and skips ls if no matching files
    emptyglob ls *.deb  # may run ls with no arguments
    noglob ls *.deb     # passes the literal * to ls
    
  9. zanchey commented on Jul 26, 2015

    @zanchey
    Member

    But it seems at least on version 2.2.0 the command is executed

    Yes, and this is driving me up the wall.

  10. modified the milestones: , fish-future on Jul 26, 2015
  11. added
    bug
    on Feb 15, 2016
  12. faho commented on Feb 15, 2016

    @faho
    Member

    Fixed by merging #2719. Thanks!

  13. added
    release notes
    on Feb 16, 2016
  14. locked as resolved and limited conversation to collaborators on Apr 18, 2020
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

    bugrelease notes

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions