You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Handling of unmatched file globs should match other shells, return non-zero? #1920
$ 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 ...
unlinkfileecho$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
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.
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?
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 :)
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
Activity
zanchey commentedon Jan 30, 2015
What's fun is that
dashandbashjust pass the unexpanded wildcard, which means thatecho *.foo > /dev/nullreturns 0 on those shells.zshsets the return status to 1 if there is no match on the glob, though.geoff-nixon commentedon Jan 30, 2015
Yeesh. Tons o' fun.
So looking at POSIX
man 3 globand the linked Patterns Used for Filename Expansion, it does seem as though passing the unexpanded glob is the specified behavior:To buttress what you're already said:
bashanddash,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 *.fooandcat *.fooreturn 1, and print[rm|cat]: *.foo: No such file or directoryto stderr (quashed with2>/dev/null)rm -rf *.fooandecho *.fooreturn 0:rm -rfis silent, andecho *.fooechoes*.foo.zshis the only other shell that aggressively errors out on the glob, in which case, it returns non-zero; and always printszsh: no matches found: *.foo,2>&1 >/dev/nullbe damned.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/nullshould quash any error, and it should return 1.Neh?
zanchey commentedon Feb 2, 2015
Sounds fine to me. The quashing of stderr is covered by #1674. The POSIX behaviour is definitely bad.
anordal commentedon Apr 7, 2015
Bash's
shopt -s nullglobis 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:
This script fails unnecessarily, because of the dreaded No matches for wildcard '*.foo'
ridiculousfish commentedon Apr 7, 2015
The
set foos *.foocase is a valid point. But consider a case likels *.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?
oconnor663 commentedon Jul 24, 2015
@ridiculousfish The fish documentation specifies:
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 :)oconnor663 commentedon Jul 24, 2015
Zsh offers commands like
noglobto 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:zanchey commentedon Jul 26, 2015
Yes, and this is driving me up the wall.
faho commentedon Feb 15, 2016
Fixed by merging #2719. Thanks!