Quietly clobbering with GNU sed

Software

Here's something fun my boss and I discovered today. Say you have this file:

$ cat test.txt
==> Zettai ryouiki in all the examples

And you want to replace a word with sed. You see GNU sed has a quiet flag, so thinking it means surpress warning messages and such (useful in scripts), you run this:

$ sed --quiet -i '' 's/all/some/g'

Checking the file again, you’re astonished to find:

$ cat test.txt
==> *crickets*

There’s nothing there. Kaput. It has ceased to exist, like a legendary feathered creature. Exactly what you’d expect for a function called quiet?

Not to get all Malcom Gladwell on you, but turns out the GNU folks decided quiet would be used in this context:

-n
--quiet
--silent

By default, sed prints out the pattern space at the end of each cycle through the script (see How sed works). These options disable this automatic printing, and sed only produces output when explicitly told to via the p command.

A perfectly reasonable, useful function. But I feel anything with the capability to clobber a file should be given a command with a less innocuous name.

sed on my beloved BSDs doesn’t have a quiet flag, and therefore there’s no such convenience/confusion.

Author bio and support

Me!

Ruben Schade is a technical writer and infrastructure architect in Sydney, Australia who refers to himself in the third person. Hi!

The site is powered by Hugo, FreeBSD, and OpenZFS on OrionVM, everyone’s favourite bespoke cloud infrastructure provider.

If you found this post helpful or entertaining, you can shout me a coffee or send a comment. Thanks ☺️.