Bash: What does ">|" do?

I have just seen this written down;

$ some-command >| /tmp/output.txt

Vertical pipes are used in standard redirects “piping” the output of one command to another, is >| in fact completely useless as it would be the same as just > in this scenario?

Asked By: jwbensley

||

It’s not useless – it’s a specialised form of the plain > redirect operator (and, perhaps confusingly, nothing to do with pipes). bash and most other modern shells have an option noclobber, which prevents redirection from overwriting or destroying a file that already exists. For example, if noclobber is true, and the file /tmp/output.txt already exists, then this should fail:

$ some-command > /tmp/output.txt

However, you can explicitly override the setting of noclobber with the >| redirection operator – the redirection will work, even if noclobber is set.

You can find out if noclobber is set in your current environment with set -o.

For the historical note, both the “noclobber” option and its bypass features come from csh (late 70s). ksh copied it (early 80s) but used >| instead of >!. POSIX specified the ksh syntax (so all POSIX shells including bash, newer ash derivatives used as sh on some systems support it). Zsh supports both syntaxes. I don’t think it was added to any Bourne shell variant but I might be wrong.

Answered By: D_Bye
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.