3 ms·
What is the difference between `sponge` and redirecting with `>` (or `>>`)? Is it about better compatibility with pipes or something?
by blauditore 6y ago
What is the difference between `sponge` and redirecting with `>` (or `>>`)? Is it about better compatibility with pipes or something?
- deleted 6y ago[deleted]
- JoelMcCracken 6y agoWell one thing is that if you’ve tried it you’ll run into the problem he illustrates there that you are writing to the Sam file you are reading from, which won’t work right. My guess is sponge buffers all the input and then sends it to output once stdin is closed.
- deleted 6y ago[deleted]
- circular_logic 6y agoI wondered this too so looked at the man page [1] > Unlike a shell redirect, sponge soaks up all its input before opening the output file. This allows constricting pipelines that read from and write to the same file. [1] https://linux.die.net/man/1/sponge https://linux.die.net/man/1/sponge
- jleahy 6y agoImo sponge is the most useful of all of them. For example if you want to filter a file in place you can do ‘grep str file | sponge file’. If you do that with a redirect you’ll end up with an empty file. Some commands (eg. sed) have an in-place option, but many don’t.