Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    

1 Answer 1

up vote 155 down vote accepted

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.

share|improve this answer
1  
Great answer, prompt and clear. Thanks very much! :D –  jwbensley Aug 10 '12 at 16:13
1  
I have also seen the symbol >! with the same meaning (using zsh) –  PPC Aug 10 '12 at 17:01
4  
It's equivalent to >! in csh and tcsh. –  Keith Thompson Aug 10 '12 at 17:13
2  
Wow. Learned something new today after more than 15 years of using linux... –  Axel Aug 11 '12 at 10:11
2  
@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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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