Do not read and write one file in a pipeline

`grep foo file > file` truncates the file before grep reads it — you lose everything.

Problem

Shell redirections are processed left-to-right and the target of `>` is truncated immediately, before the command on the left runs. Reading and writing the same file in one pipeline is a classic way to destroy data — and ShellCheck flags it.

Bad

grep -v foo log.txt > log.txt   # log.txt is empty now

Good

# Use a temp file:
grep -v foo log.txt > log.txt.tmp && mv log.txt.tmp log.txt

# Or sponge from moreutils:
grep -v foo log.txt | sponge log.txt

# Or sed/ed in-place:
sed -i '/foo/d' log.txt

Explanation

`sponge` (from moreutils) buffers stdin in memory and writes only when EOF is reached — perfect for this. `sed -i` rewrites in place atomically. Either is safer than the explicit temp-file dance.

When It Matters

A pipeline starts all its commands at once, so cmd < file > file truncates the file before the reader gets to it. The classic sort file > file leaves you with an empty file and no backup, and it is unrecoverable — the data is gone the moment the shell opens the redirection. Editors and version control hide the pain of this mistake most of the time; a script running unattended over generated data does not.

Second Example

Note

mv within the same filesystem is atomic, so a reader either sees the old file or the new one — never a half-written mixture.

Exceptions

Reading and appending to the same file (>>) does not truncate, but it can loop forever if the reader keeps catching up with the writer. sed -i and moreutils sponge exist precisely so that in-place edits are safe; use them rather than disabling the warning.

Faq

Q

Why is the file empty afterwards?

A

The shell sets up redirections before running the command, and > truncates the target immediately. The reading command then finds a zero-byte file.

Q

Is sed -i atomic?

A

GNU sed writes a temporary file and renames it, so the replacement is atomic, but the original is only preserved if you supply a suffix such as -i.bak.

Q

Does mktemp need cleanup?

A

Yes — register a trap to remove it, so an early exit does not leave temporary files behind.