Read and Write the Same File in Bash
I tried to read and write the same file in a pipeline, and got caught out by a race condition (why is the file empty?!). Do this instead:
some_script < file > smscrpt.$$ \ && mv smscrpt.$$ file || rm smscrpt.$$
|| removes the temporary file if it errors.
$$ is the process ID and ensures that you always have a unique temporary file name.