Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

>> "That doesn't help with sed -i and similar things."

> Can you give an example? I don't know what you mean.

Sure. The first line below is dangerous no matter what zsh does to save you from yourself. The second line is safe no matter which shell you are using, and no matter what other commands are in the pipeline:

    sed $SEDOPTIONS "s/$SEARCHTERM/$REPLACEMENT/g" $FILENAME

    cat $FILENAME | sed $SEDOPTIONS "s/$SEARCHTERM/$REPLACEMENT/g"

>> "Zsh stops redirection errors"

> Which is what the post I was answering to was complaining about, wasn't it?

That's not how I interpreted "a typo in the rest of the pipeline." Sure, the typo could be a redirection. It could also accidentally set $SEDOPTIONS in the example above to include the '-i' flag.

>> "it won't help even if cat is in the front"

> Why not? "cat > foo" will error out if "foo" exists and NO_CLOBBER is set.

Yes, "cat > foo" will error out, but "cat $FILENAME | sed -i "s/a/b/g" $FILENAME" won't.



Yes, in-place editing with tools like sed is dangerous.

But, in your own example you have a useless use of cat:

  cat $FILENAME | sed $SEDOPTIONS "s/$SEARCHTERM/$REPLACEMENT/g"
could be replaced with:

  sed $SEDOPTIONS "s/$SEARCHTERM/$REPLACEMENT/g" < $FILENAME


Well, that's the whole point of the useless use of cat - in the non-cat example you gave, making a typo at the end of the command destroys my data.

With the useless use of cat, that is no longer possible even if there is a typo in the SEDOPTIONS.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: