2 ms·
How might one use sponge in a way that shell redirection wouldn’t be more fully-featured? The best I can currently think of is that it’s less cumbersome to wrap
by jchw 5y ago
How might one use sponge in a way that shell redirection wouldn’t be more fully-featured? The best I can currently think of is that it’s less cumbersome to wrap (for things like sudo.)
- JoshTriplett 5y agoSponge exists for cases where shell redirection wouldn't work, namely where you want the source and sink to be the same file. If you write: somecmd < somefile | othercmd | anothercmd > somefile the output redirection will truncate the file before it can get read as input. Sponge "soaks up" all the output before writing any of it, so that you can write pipelines like that: somecmd < somefile | othercmd | anothercmd | sponge somefile
- jchw 5y agoThank you, I missed this bit of nuance. That indeed would be useful, and now the example makes a lot more sense.
- scbrg 5y agoThis can be done with regular shell redirection, even though I wouldn't recommend it. Easy to get wrong, and fairly opaque: $ cat foo foo bar baz $ ( rm foo && grep ba > foo ) < foo $ cat foo bar baz $