3 ms·
I see sponge is somehow useless, how is `| sponge /etc/passwd` different from `> /etc/passwd`.
by kachnuv_ocasek 15y ago
I see sponge is somehow useless, how is `| sponge /etc/passwd` different from `> /etc/passwd`.
- ominous_prime 15y agoThe redirect happens before the command is executed, so if you're trying to read from /etc/passwd, it's already been overwritten. Sponge will buffer the output and write the file after the first command has executed.
- kachnuv_ocasek 15y agoAh, excuse my ignorance.
- dedward 15y agoHe explained it on the site - if you are trying to use grep to trim contents from a file and output them to the same file, you will end up with an empty file. Seriously though - everyone knows this. Just use a temp file. He says sponge "keeps the results in memory" - that's a problem if the file is huge. Plus there are other tools to do in-place replacement.