You add one line to a log and discover yesterday's lines are gone. In echo "ready" > run.log, the single > may have replaced the file's contents. What would change with >>?
What is the difference between > and >>?
> redirects a command's standard output to a file. If the file exists, the shell truncates it before writing. >> also redirects output, but appends to the end instead. Both create the file if it does not exist.
| Symbol | Existing file | Typical use |
|---|---|---|
> | Replaces its contents | A freshly generated result |
>> | Adds output at the end | A record built line by line |
These symbols are shell redirection, not options specific to echo. They behave the same way when redirecting standard output from printf or another command.
The diagram's “input file” means the file's previous state, not a separate input file. Starting with boot and ready, writing retry with > leaves only retry; >> adds retry after the existing lines.
What remains after writing to the same file?
Use mktemp to create a temporary file instead of modifying an existing file. The variable holds its path.
demo_file=$(mktemp)
echo "boot" > "$demo_file"
echo "ready" >> "$demo_file"
cat "$demo_file"boot
ready>> appended ready. Now write to that same file with >:
echo "retry" > "$demo_file"
cat "$demo_file"retryThe original lines are gone. Read the file after each write to see the difference directly.
Can > empty a file even when the command fails?
Yes. The shell opens and truncates the output file before running the command. If you try to read a missing input file and redirect the result to an existing output file, the read can fail after that output file has already been emptied.
echo "keep" > "$demo_file"
cat missing-input-for-demo.txt > "$demo_file"
cat "$demo_file"Assuming missing-input-for-demo.txt does not exist, the second command reports an error and the final cat prints nothing. Try this only with the temporary file, never with an important output file.
Changing > to >> preserves the previous line, but it does not make the failed command succeed. Retries can also append duplicate lines. Avoiding an overwrite and producing a correct result are different concerns.
Which one should you use for logs or results?
> may be right for a report regenerated from scratch. If the old result matters, generate a new file separately and replace the old one only after success. >> fits a simple line-by-line record, but consider duplicate entries on retry and unbounded file growth.
Before choosing a symbol, ask whether the file is a new result each time or a continuing record. If the destination path is uncertain, check the current directory and filename before writing. A single > can empty the wrong file.
Key takeaways
> overwrites an existing file; >> appends to it. The shell handles both, and > can truncate the file even if the command later fails. Avoid writing directly over an important file before verifying that the new result is complete.

