Redirection happens before the command
When bash sees command > file, it does not hand the file name to the command. The shell opens the file itself, in the child process that will run the command, and makes the opened file the new standard output. Only then does the program start. The program receives a file descriptor that already points at the destination, and it has no idea a redirection took place.
This ordering is the source of the most common surprise. The file is opened for writing with truncation, so its old contents are discarded as soon as the shell processes the redirection. If the command then fails, reads its own input from that same file, or prints nothing, the damage is already done.
printf 'keep me\n' > data.txt
cat data.txt > data.txt
cat data.txtThe second line truncates data.txt before cat reads it, so the final cat prints nothing. The first cat received an empty file from the shell, and the original text is gone. This is why reading and writing the same file in one command is almost always a mistake.
Appending with >>
The operator >> opens the file for appending. Existing content is kept, and new output is written after the last byte. Use it for logs and for collecting results from several commands into one file.
echo first > log.txt
echo second >> log.txt
cat log.txtThe output lists first and then second. Had the second line used a single >, only second would remain, which is a common accident when a script is meant to accumulate output.
Creating an empty file
A redirection with no command creates or truncates a file. The line > file alone is valid, and it is the standard way to reset a log in a script. Because the shell opens the file with truncation, the same mechanism that destroys data is also the reliable way to create an empty file that already exists or does not exist yet.
> empty.txt
wc -c empty.txtThe byte count is 0. Whether empty.txt existed before or not, it now holds nothing.
Protecting files with noclobber
Bash has an option that makes > refuse to overwrite an existing regular file. It is called noclobber, and you turn it on with set -o noclobber. When it is active, a plain > to an existing file fails with an error, and you must use >| to override it deliberately. Appending with >> is still allowed, because it does not discard anything.
set -o noclobber
echo one > guarded.txt
echo two > guarded.txt; echo "status=$?"
echo two >| guarded.txt
set +o noclobberThe second write fails with a message that the file exists and the status is 1, so guarded.txt still contains only one. The >| form writes two, and the final command turns the option off again. Many people enable noclobber in their interactive shells as a safety net against typing > where they meant >>.
noclobber protects against accidental overwrites from the shell, but it does not stop a program from truncating a file it opens itself. A script that needs safety should check for the file or write to a temporary name first.
Redirecting several commands at once
Putting a redirection after a group of commands in braces applies it to the whole group. The shell opens the file once, before the first command in the group runs, and every command writes into the same open file. This is cleaner than repeating the redirection on each line, and it avoids the truncation trap that a second > would cause.
{ echo header; echo body; } > report.txt
cat report.txtThe file holds both lines in order. The braces group must end with a semicolon or a newline before the closing brace, which is why the example has a semicolon after body. Groups are also how you redirect a whole block of a script that prints many lines.
Writing a file safely
A safe pattern for generating a file is to write to a temporary name in the same directory, then rename it into place. The rename is atomic on the same file system, so readers see either the old complete file or the new complete file, never a half-written one. The shell itself does not do this for you, so it is a habit you build in scripts.
printf 'report\n' > report.tmp && mv report.tmp report.txt
cat report.txtThe output is report. The && makes the move happen only if the write succeeded, so a failed write leaves the previous report in place.
Redirecting into a directory or a missing path
A redirection needs a place to write. If the directory in the path does not exist, the shell reports an error and does not run the command. The command itself is never started, which is useful to know when you see no output at all.
echo hi > nodir/out.txt; echo "status=$?"The shell prints a message about the missing directory, and the status is 1. Nothing is created, and echo never ran. A failed redirection is reported before the command starts, so reading the error message first saves confusion.