Take the 2-minute tour ×
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.

Do you have a mnemonic or system? This has bothered me for years I always have to look it up

share|improve this question

closed as primarily opinion-based by terdon, Patrick, slm, Ramesh, vonbrand Jun 19 at 17:12

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

7 Answers 7

up vote 20 down vote accepted

If you are a C programmer, you can think of &1 as "the address of 1" so 2>&1 reads "redirect file descriptor #2 to the same place as #1".

share|improve this answer
    
Don't know if it helps... Mnemonics are quite a personal stuff. –  lgeorget Jun 19 at 14:05
    
+12 That's also a correct description of what's happening. –  Stéphane Chazelas Jun 19 at 14:50
1  
I wonder if it's how it was thought of ("the address of 1") when it was invented. As some people contributed heavily to both the C language and UNIX when they were invented, it's possible –  lgeorget Jun 19 at 15:03
1  
I might start using this one instead ;) –  goldilocks Jun 19 at 15:39

My coworkers and I usually say "two is greater than one", because most of us don't always remember that stderr is file descriptor 2 and stdout is 1 (especially those new to unix/linux), so the other mnemonics about redirecting stderr don't really work. Only problem is, you still have to remember where the ampersand goes!

share|improve this answer

When you write 2>&1, you're saying "standard error goes to standard output".

Let's break that down.

First you want to memorize that standard error is 2 and standard output is 1.

So you've got 2 something something 1.

"goes to" is written >.

So you've got 2> something 1.

2>filename means send standard error to filename. But you don't want to send it to a file called 1. You want something else: the number of a file that's already open. That's what the & is for.

So 2>&1.

You can also think of it like you were doing an assignment, where the > is like an equals and the & is like a $, compare:

f=$1
2>&1

To understand a command line with multiple redirections, the important thing to know is that the redirections are done left to right. See Order of redirections for more details about that.

share|improve this answer

Not a mnemonic, but I read it as follows:

0 is stdin. 1 is stdout. 2 is stderr. > is into. < is out of. & is file descriptor (in some shells).

2>&1
2      >    &               1
stderr into file descriptor 1

redirect stderr into stdout

It might change if you have messed with any of the file descriptors prior to the redirection...

2>somefile 1>&2
2      >    somefile     1      >    &2
stderr into somefile and stdout into file descriptor 2

redirect stderr into somefile and stdout into somefile. 
share|improve this answer

If you understand that FD2 is STDERR, you might think, "Oh, and capture STDERR where ever I send STDOUT (FD1)".

share|improve this answer

I remembered that it is always 2 -> 1. Stderr to stdout.

The middle part is always the hard one and I always messed it up, until I remembered that first comes the sharp character >, then the character I can't write in real life &.

So never 2&>1, always 2>&1

share|improve this answer

"Two to and one" ("to" being >) makes more logical sense to me than "Two and to one", which is what I might usually confuse it with. If you consider "and one" as a single noun (a place), it also makes grammatical sense in context, which is harder to do with "Two and to one" -- you'd have to consider "to one" a single noun, and it still would not make contextual sense.

share|improve this answer
2  
I don't understand –  mtm Jun 19 at 22:40

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