Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Dangerous feature - multios with globbing



On Sat, Sep 21, 2024 at 6:26 PM Nadav Har'El <nyharel@xxxxxxxxx> wrote:
>
> Hi, I've been a very happy zsh user for the last 30 years (yes, really :-) before that, I had used ksh).
> Today I stumbled across a dangerous feature that I have never noticed before. I *wanted* to type:
>
>      xv *
>
> In a directory with 4000 photos, to view them.
> But by mistake, I typed:
>
>     xv > *
>
> Note the extra ">".
>
> This mistake should have been benign - ksh says in this case "ksh: *: Ambiguous", and bash says "bash: *: ambiguous redirect", and creating a file called "*" would also be fine. But I wasn't that lucky :-( What actually happened was that all my files - 4000 of them - were truncated to size 0!
>
> It turns out that this dangerous feature is actually documented in the zsh manual page:
>
>        If  the MULTIOS option is set, the word after a redirection operator is
>        also subjected to filename generation (globbing).  Thus
>               : > *
>        will truncate all files in the current directory, assuming  there's  at
>        least  one.  (Without the MULTIOS option, it would create an empty file
>        called `*'.)
>
> But the fact that this behavior is documented doesn't make it any less dangerous, and to be honest - it makes absolutely no sense to me.
>
> I quickly added "set +o multios" in my ~/.zshrc so this will never happen to me again - at the cost of losing the multios feature completely.
> But maybe just this multios-plus-globbing feature should be disabled (or made a separate option, disabled by default) so that future zsh users don't loose 4000 of their photos, or don't need to disable the multios feature?

Another option is setopt NOCLOBBER, then you would have gotten this result:
% true > *
zsh: file exists: a
(unless you accidentally type >! * sometimes too)

-- 
Mikael Magnusson




Messages sorted by: Reverse Date, Date, Thread, Author