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

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.)


Sponge 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


This 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
    $


Thank you, I missed this bit of nuance. That indeed would be useful, and now the example makes a lot more sense.




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

Search: