A small script writes a manifest of the camera clips that went up that day: one file per day, one clip name per line. Later steps read that file to decide what can be cleared off the phone. One afternoon the script ran a second time. The cleanup had already removed the first two clips and a new one had been recorded, so the second run wrote a shorter list over the first:
#!/bin/bash # Record which camera clips went up today. out="manifests/uploaded_2026-11-30.txt" mkdir -p manifests ls clips > "$out" echo "wrote $(wc -l < "$out") names to $out" $ touch clips/clip_01.mp4 clips/clip_02.mp4 clips/clip_03.mp4 clips/clip_04.mp4 $ bash manifest.sh wrote 4 names to manifests/uploaded_2026-11-30.txt $ rm clips/clip_01.mp4 clips/clip_02.mp4; touch clips/clip_05.mp4 $ bash manifest.sh; echo "exit status: $?" wrote 3 names to manifests/uploaded_2026-11-30.txt exit status: 0 $ cat manifests/uploaded_2026-11-30.txt clip_03.mp4 clip_04.mp4 clip_05.mp4 Exit status zero, and the record of clip_01 and clip_02 going up is gone. Nothing here is a bug in the usual sense. > truncates, and that is its job.
Bash has an option for exactly this mistake: set -o noclobber (short form set -C). I added it, expecting the second run to fail. It did not fail. It did something more confusing, and while working out why, I found three more places where the option does less than its name suggests. Every block below was run in GNU Bash 5.1.16 on Linux, and every output block is pasted unedited from the run.
What the manual promises
The Redirections chapter of the Bash manual says that with > and noclobber on, "the redirection fails if the file whose name results from the expansion of word exists and is a regular file." The same paragraph names the way around it: with >|, "Bash attempts the redirection even if the file named by word exists."
So the promise is narrow. It is about one operator, >, and one kind of target, an existing regular file. Everything else in this post is a consequence of that sentence.
Test one: the guard holds and the script keeps going
One line changed, near the top:
#!/bin/bash # Record which camera clips went up today. set -o noclobber out="manifests/uploaded_2026-11-30.txt" mkdir -p manifests ls clips > "$out" echo "wrote $(wc -l < "$out") names to $out" $ touch clips/clip_01.mp4 clips/clip_02.mp4 clips/clip_03.mp4 clips/clip_04.mp4 $ bash manifest_nc.sh wrote 4 names to manifests/uploaded_2026-11-30.txt $ rm clips/clip_01.mp4 clips/clip_02.mp4; touch clips/clip_05.mp4 $ bash manifest_nc.sh; echo "exit status: $?" manifest_nc.sh: line 6: manifests/uploaded_2026-11-30.txt: cannot overwrite existing file wrote 4 names to manifests/uploaded_2026-11-30.txt exit status: 0 $ cat manifests/uploaded_2026-11-30.txt clip_01.mp4 clip_02.mp4 clip_03.mp4 clip_04.mp4 The guard held. The manifest still lists all four clips from the first run, which is what I wanted. But read the second run's output from the bottom: exit status: 0, and just above it, wrote 4 names. The script reported a write that did not happen. It counted the lines of the old file and printed that count as if it had produced it.
A refused redirection is an ordinary failed command. Bash skips the command it was attached to (ls did not run), prints one line to stderr, sets the status to 1 and carries on with the next line. In a cron log that keeps only stdout, the one honest line is the one nobody reads.
Test two: making the refusal stop the script
Two ways to turn the refusal into a stop. The first adds set -e on the same line as the option. The second keeps errexit off and checks the redirection itself, which also gives a place to say what happened in words a later reader will understand:
#!/bin/bash # Record which camera clips went up today. set -e -o noclobber out="manifests/uploaded_2026-11-30.txt" mkdir -p manifests ls clips > "$out" echo "wrote $(wc -l < "$out") names to $out" #!/bin/bash # Record which camera clips went up today. set -o noclobber out="manifests/uploaded_2026-11-30.txt" mkdir -p manifests if ! ls clips > "$out"; then echo "$out already exists; leaving it alone" >&2 exit 1 fi echo "wrote $(wc -l < "$out") names to $out" $ touch clips/clip_01.mp4 clips/clip_02.mp4 clips/clip_03.mp4 clips/clip_04.mp4 $ bash manifest_e.sh wrote 4 names to manifests/uploaded_2026-11-30.txt $ bash manifest_e.sh; echo "exit status: $?" manifest_e.sh: line 6: manifests/uploaded_2026-11-30.txt: cannot overwrite existing file exit status: 1 $ bash manifest_check.sh; echo "exit status: $?" manifest_check.sh: line 6: manifests/uploaded_2026-11-30.txt: cannot overwrite existing file manifests/uploaded_2026-11-30.txt already exists; leaving it alone exit status: 1 Both versions stop with exit status 1 and leave the morning list alone. In the second, Bash's own message still appears above mine. The redirection fails before ls exists as a running command, so the message comes from the shell itself, not from anything the if can hide.
The point is that noclobber only decides whether the file gets truncated. What the script does after the refusal is still up to the script.
Test three: setting it in your shell does not set it in the script
Before I put the line in the script, I had turned the option on in my own terminal and run the job from there, assuming the job would inherit it. That is how most people meet noclobber: a line in an interactive startup file, protecting fat-fingered > at the prompt.
$ touch clips/clip_01.mp4 clips/clip_02.mp4 clips/clip_03.mp4 $ mkdir -p manifests; echo "earlier list" > manifests/uploaded_2026-11-30.txt $ set -o noclobber $ echo "typed by hand" > manifests/uploaded_2026-11-30.txt bash: line 8: manifests/uploaded_2026-11-30.txt: cannot overwrite existing file $ bash manifest.sh wrote 3 names to manifests/uploaded_2026-11-30.txt $ echo "earlier list" >| manifests/uploaded_2026-11-30.txt $ export SHELLOPTS $ bash manifest.sh; echo "exit status: $?" manifest.sh: line 5: manifests/uploaded_2026-11-30.txt: cannot overwrite existing file wrote 1 names to manifests/uploaded_2026-11-30.txt exit status: 0 $ cat manifests/uploaded_2026-11-30.txt earlier list At the prompt the guard worked. bash manifest.sh started a new, non-interactive Bash, which began with noclobber off, and it overwrote the file without a word. In a separate run with the option in a throwaway ~/.bashrc, the result matched: an interactive bash -i refused the write, and bash manifest.sh from the same home directory overwrote it. A script run that way does not read ~/.bashrc.
The second half of that block is the documented way to pass shell options down. The manual's entry for SHELLOPTS says: "If this variable is exported, child shells will enable each option in the list." After export SHELLOPTS, the child started with noclobber on and refused. That also exported every other option that was on at the time, so I would not rely on it for a scheduled job. The job should say what it needs on its own first lines, where the next person to read it will see it.
Test four: what the guard stops and what walks past it
The manual names one operator. To see the edges, this script tries nine ways of writing to an existing file with noclobber on, and prints what the file holds afterwards:
#!/bin/bash set -o noclobber try() { printf '%-40s' "$1" if eval "$1" 2>/dev/null; then echo "ran f.txt: $(paste -sd' ' f.txt)" else echo "refused f.txt: $(paste -sd' ' f.txt)" fi } echo original > f.txt echo from-cp > g.txt try 'true > f.txt' try 'true 2> f.txt' try 'true &> f.txt' try '( true > f.txt )' try 'x=$(true > f.txt)' try 'echo appended >> f.txt' try 'echo from-tee | tee f.txt >/dev/null' try 'cp g.txt f.txt' try 'echo forced >| f.txt' $ bash redirects.sh true > f.txt refused f.txt: original true 2> f.txt refused f.txt: original true &> f.txt refused f.txt: original ( true > f.txt ) refused f.txt: original x=$(true > f.txt) refused f.txt: original echo appended >> f.txt ran f.txt: original appended echo from-tee | tee f.txt >/dev/null ran f.txt: from-tee cp g.txt f.txt ran f.txt: from-cp echo forced >| f.txt ran f.txt: forced The first five rows are all the same operator in different clothes. 2> and &> are output redirections too, and a subshell or a command substitution inherits the option from the script that started it, so all five were refused.
The last four rows wrote:
-
>>appends. It does not truncate, so there is nothing for the guard to protect. If the manifest step used>>, a second run would have doubled lines instead of losing them, and noclobber would not have noticed either way. -
teeis a program, not a redirection. It opens its file argument itself, and the shell option does not reach it. A pipeline ending intee manifest.txttruncates the file whether noclobber is on or not. -
cpis a program too. The same goes for anything else that takes an output file name as an argument. For whatmvdoes with a name that already exists, see my test of same-name moves. -
>|overrides on purpose. It is the escape hatch the manual describes, and it is the one I keep in scripts where overwriting is the point.
Test five: a link to nothing still counts as taken
One more case, because it went against my reading of the manual's regular-file wording. A script that keeps a latest link to the newest clip can end up with the link pointing at a clip that has not been written yet:
$ ln -s clips/clip_09.mp4 latest.mp4 $ test -e latest.mp4 && echo "target exists" || echo "target missing" target missing $ set -o noclobber $ echo data > latest.mp4; echo "status: $?" bash: line 8: latest.mp4: cannot overwrite existing file status: 1 $ echo data >| latest.mp4; echo "status: $?" status: 0 $ cat clips/clip_09.mp4 data test -e follows the link and reports that nothing is there. Bash still refused >, with the same message it gives for a real file. >| followed the link and created the clip. I am reporting what this Bash did, not the reason: a dangling link is not a regular file, and the manual's sentence does not cover it either way. If your script writes through links, test this case on the machine that runs it.
What I changed in the manifest script
- The option lives in the script.
set -o noclobber(orset -C) goes on the script's own first lines, not in a startup file and not in whatever shell launched the job. - The refusal stops the run. Either
set -ealongside it, or an explicitif !around the redirected command, with a message that says the file was left alone. - Deliberate overwrites say so. A status file that is meant to be rewritten every run uses
>|, so a reader can tell intent from accident at a glance. -
teeandcpget their own check. Where one writes the manifest, atest -eon the path comes first. That test has a gap between checking and writing, which is fine for a once-a-day job and not fine for two jobs racing for one file. - The log keeps stderr. The single line that said the write was refused was the one true line in that run's output.
What these runs do not cover
- One Bash. Everything above is GNU Bash 5.1.16. The quotes are from the current online manual. Nothing disagreed, but no other version was run.
- Other shells. dash, zsh and ksh have their own versions of this option. None were tested.
- Concurrent runs. Every test ran one script at a time. Whether noclobber is safe as a lock between two scripts starting together was not tested and is not claimed here.
- Network file systems. All files were on a local disk.
- Toy clips. The clips are empty files made with
touch. Nothing was uploaded or deleted except by the commands shown.
Sources. Quotes come from Redirections and Bash Variables in the GNU Bash Reference Manual. Each output block was produced by a small runner that echoes every command before executing it in a fresh, empty folder.
The clips in a folder like this come from somewhere. Mine come from an old phone running Background Camera RemoteStream on Google Play, an Android app I make that records with the screen off. The scripts above work the same on any folder of files.
set -o noclobber stops one operator, >, from truncating one kind of target. It does not stop the script, it does not travel into a child Bash, and it does not reach tee or cp. A guard that refuses a write while the job reports success has protected the file and hidden the failure at the same time.

