3

What is the difference between these two redirections?

[localhost ~]$ echo "something" > a_file.txt
[localhost ~]$ echo "something" >| a_file.txt

I can't seem to find any documentation about >| in the help.

1 Answer 1

3
+50

>| overrides the noclobber option in the shell (set with $ set -o noclobber, indicates that files can not be written over).

Basically, with noclobber, you get an error if you try to overwrite an existing file using >:

$ ./program > existing_file.txt
bash: existing_file.txt: cannot overwrite existing file
$

Using >| will override that error and force the file to be written over:

$ ./program >| existing_file.txt
$

It's analogous to using the -f or --force option on many shell commands.

From the Bash Reference Manual Section "3.6.2 Redirecting Output":

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 is not enabled, the redirection is attempted even if the file named by word exists.

Searching for "bash noclobber" generally brings up articles that mention this somewhere. See this question on SuperUser, this section in O'Reilly's "Unix Power Tools", and this Wikipedia article on Clobbering for examples.

0

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