Bash – What does redirection with >| do

bashio-redirectionshell

When should you use >| for redirecting output to a file instead of just >?

The explanation given when I was shown this was that it guaranteed the target file was truncated first. But I thought > already implied that.

Is >| useful?

Best Answer

It's an explicit bypass of noclobber option.

That way, you can overwrite the file even with noclobber set.

See http://mywiki.wooledge.org/NoClobber

From man bash :

If the redirection operator is >, and the noclobber option to the set builtin has been enabled, the redirection will fail if the file whose name results from the expansion of word exists and is a regular file. If the redirection operator is >|, or the redirection operator is > and the noclobber option to the set builtin command is not enabled, the redirection is attempted even if the file named by word exists.

Related Question