APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds RSS Feeds











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version
->
-> 2>&1


2>&1



Shell redirection, and the "2>&1" is probably the most common form. That simply means to send stderr to wherever stdout is pointing. An easy way to demonstrate all this is to create a direcory and put one file in it:

mkdir tt
cd tt
touch t
 

That gives us a known situation to work with. Sitting in that directory,

ls foo t > y
 

Puts "t" in "y", but displays "ls: foo: No such file or directory" on the screen. That's because ls writes what it doesn't understand to stderr (file descriptor 2). If we want both things in y, we just do:

 ls foo t >y  2>&1 
 

Or, if you wanted the error output in a file as you might with "cc", for example:

cc source.c 2>/tmp/errors
 

There are lots of ways to do this sort of thing. More interesting is the case of a shell script where you want to capture stderr but let any "ok" output go to the screen. To do that, take advantage of other file descriptors:

x=`ls foo t 3>&2 2>&1 1>&3`
 

$x will have the error message but "t" will go to the screen. How does that happen? Well, 3>&2 puts stderr into (normally unused) file descriptor 3. Then 2>&1 puts 1 into 2. This might be hard for you to follow, but hang in there a minute. Next, 1>&3 puts 3 (which is the original 2) into 1. Effectively, this reverses stdout and stderr. Let's take a closer look:

When the shell starts , both stderr (1) and stdout (1) point to our screen. However, the

x=`ls foo t `
 

Would leave stderr still at the screen, but stdout would redirect to our "x" variable. We want the opposite of that: The 3>&2 makes 3 the same as 2, so now 3 is going to the screen. 2>&1 makes 2 go where 1 was, which is into our "x". Finally, 1>&3 puts 1 back to the screen by copying 3.

(See exec also)




If this page was useful to you, please help others find it:  





2 comments




More Articles by - Find me on Google+



Click here to add your comments
- no registration needed!
"Shell redirection, and the "2&amp;>1" is probably the most common form."

Er, you meant 2>&amp;1, right?

--BigDumbDinosaur


Ayup :-)

--TonyLawrence


Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar

Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

I am a Kerio reseller. Articles here related to Kerio products reflect my honest opinion, but I do have an obvious interest in selling those products also.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.

pavatar.jpg

This post tagged:

       - Scripting
       - Shell
       - Unix Words















My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide



Buy Kerio from a dealer
who knows tech:
I sell and support

Kerio Connect Mail server, Control, Workspace and Operator licenses and subscription renewals



Click and enter your name and phone number to call me about Kerio® products right now (Flash required)