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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x