TIL: In place file updates in Bash
2021-09-09
- bash
I’ve learned this one several times over the years so I’m finally writing it
down. And this time I learned about a GNU utility called sponge
that addresses
this exact problem. It’s not always available though.
This doesn’t work:
echo test > foo
cat foo > foo
You’ll end up with an empty file. The solution is to either write to a temp
file and write over the original, or use sponge
:
echo test > foo
cat foo | sponge foo
You can get sponge (and some other goodies) on a Mac with:
brew install moreutils