Tell me more ×
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

2 Answers

up vote 133 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 – javano 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
3  
It's equivalent to >! in csh and tcsh. – Keith Thompson Aug 10 '12 at 17:13
1  
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

It's an IO redirection operation. You have :

stdin 0
stdout 1
stderr 2

in UNIX world, You can redirect output of the your command to a file. such as:

 ls -l > myfile.txt

Above command used stdout.

or :

ls -l >> myfile.txt

Above command doesn't overwrite a file; it just appends to end of the file, or creates it if it doesn't exist.

Many commands have standard error: if you want to redirect standard error you just use 2>

Suppose, you want to read a file for which you don't have permission. It's a error. If you are a normal user, use:

cat /etc/passwd 2> myerror.txt

Sometimes you want to redirect standard error and standard out to the same place. You may for example:

ls -R /etc 2>&1 > myoutputanderrors.txt

But Your Answer:

Redirection of output causes the file whose name results from the expansion of word to be opened for writing on file descriptor n, or the standard output (file descriptor 1) if n is not specified. If the file does not exist it is created; if it does exist it is truncated to zero size.

The general format for redirecting output is:

 [n]>[|]word

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.

share|improve this answer
6  
As you point out, it is an io redirection rather than a pipe, but you don't address the use of | which is what confused the OP. I didn't recognize this usage either as pointed out by @D_Bye. – Joe Aug 10 '12 at 20:59
Unfortunately i didn't see | in question and i think it's related to IO redirection.because i'm in code.... – Mohsen Pahlevanzadeh Aug 12 '12 at 5:23

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.