147

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?

218

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.

  • 3
    @sch - re your historical note (thanks for the extra info, btw) - FreeBSD's sh has noclobber, since about May 2002. – D_Bye Sep 24 '12 at 10:42

protected by Community Mar 12 '18 at 11:49

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.