a_file.txt [localhost ~...">

Shell redirection operation> |?

What is the difference between these two redirects?

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

      

I cannot find any documentation about> | in help .

+3


source to share


1 answer


>|

overrides an option noclobber

in the shell (set with $ set -o noclobber

, specifies that files cannot be written).

Basically, noclobber

an error pops up if you try to overwrite an existing file with >

:

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

      

Usage will >|

override this error and cause the file to write:

$ ./program >| existing_file.txt
$

      



It is similar to using options -f

or --force

for many shell commands.

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

If the redirection operator >

, and the option noclobber

for inline set is enabled, the redirection will fail if the file whose name is the result of the word extension exists and is a regular file. If a redirection operator >|

, or a redirection operator >

, and the parameter is noclobber

not included, the redirection is performed even if the file named by the word exists.

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

+3


source







All Articles