Bash’s `noclobber`: Prevent Accidental Overwrites

Quick Tip

Bash’s `noclobber`: Prevent Accidental Overwrites

Challenge: When redirecting output in shell scripts or on the command line, you might accidentally overwrite existing files with output from a command. This can lead to data loss.

The Solution: Use the `set -o noclobber` shell option to prevent accidental overwrites when redirecting output using `>`.

set -o noclobber

Why it works: With `noclobber` enabled, if you try to redirect output to a file that already exists, the shell will throw an error instead of overwriting the file. This acts as a safety net.

Pro-Tip: You can temporarily bypass `noclobber` for a specific redirection by using `>` followed by `!`, like `command >! existing_file.txt`. To re-enable it in the same session, use `set -o noclobber` again.

Linux Tips & Tricks | © ngelinux.com | 7/2/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted