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 newly introduced one had been recorded, so the second run wrote a shorter list over the first:.
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.
Sources indicate that 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.
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.".
It has been revealed that 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.
One line changed, near the top:.
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.
In a recent development, 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:.
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.
Industry observers note that 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.
Sources indicate that 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.
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.
In a recent development, the second half of that block is the documented way to pass shell options down. The manual's entry for SHELLOPTS indicates: "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.
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:.
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.
In a recent development, 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:.
Recent findings suggest that 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.
In a recent development, 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.
In a recent development, 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.
It has been revealed that 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.
