Search code examples
bashshellzshglob

What behavior can and should I expect of a shell with the command and glob "echo -?"?


If I want to match a file called "-f" or "-r" I might do something like

test.sh -?

And if I want to send the literal '-?' to a program as an argument I might do something like:

test.sh -\?

If no such file "-f" or "-r" or anything like it exists, then what should my shell do with

test.sh -?

Should it tell me that no file matches this pattern?


Solution

  • In bash, the default is to treat an unmatched pattern literally. If the nullglob option is set, an unmatched pattern "evaporates"; it is removed from command, not even expanding to the empty string.

    In zsh, an unmatched pattern produces an error by default. Setting the nomatch option causes an unmatched pattern to be treated literally, and zsh also supports a nullglob option which causes unmatched patterns to disappear. There is also a cshnullglob option which acts like nullglob, but requires at least one pattern in a command to match, or an error is produced.

    Note that POSIX specifies that if the pattern contains an invalid bracket expression or does not match any existing filenames or pathnames, the pattern string shall be left unchanged in sh.

    ash, dash, ksh, bash and zsh all behave this way when invoked as sh.