> This allows constructing pipelines that read from and write to the same file.
TIL, how did I not know about sponge before?
TIL, how did I not know about sponge before?
cat file.txt | sed "s/a/b/g" | sponge > file.txt
Because bash opens file.txt for writing and truncates it
immediately, so "cat file.txt" wont output anything. You have to do this for it to work: cat file.txt | sed "s/a/b/g" | sponge file.txt
sponge wont open the file for writing until it's sponged everything up. If you remember this, sponge is an awesome tool in the UNIX toolbox.You don't have to use pipes either, you know. You can just save the output from every command to an intermediate file, then run the next command with that file as input.