On Thu, Oct 05, 2006 at 12:48:28PM -0400, J. Scott Olsson wrote:
> This is a sort-of-related question: anyone have a mnemonic for remembering
> how to do redirects in bash? For some reason, I can never get this to stick
> in my head.
Short answer: no... I've being mucking around with unix since I was in high
school
(13 years ago?) and I still have to mess around with it to remember. However,
if you remember that
1) a statement like " 1>&2" or "X>&Y" is really a fdup(X,Y)
2) "2> file.foo" is like an freopen(2,"file.foo")
3) statements are processed *left to right*
then it all kind of makes sense (*kinda*).
So `./foo > out 2>&1` is:
"> out" --> freopen(1,"out") -- redirect stdout to "out"
"2>&1 " --> fdup(1,2) -- redirect stderr to where
ever '1' points, which
is "out"
FWIW, I made a little shell script that I would suggest playing
with to see what happens:
[EMAIL PROTECTED] ~]$ cat t.sh
#!/bin/sh
echo This is stderr >&2
echo This is stdout
[EMAIL PROTECTED] ~]$ ./t.sh > out.out 2>out.err
[EMAIL PROTECTED] ~]$ cat out.err
This is stderr
[EMAIL PROTECTED] ~]$ cat out.out
This is stdout
[EMAIL PROTECTED] ~]$ ./t.sh > out.1 2>&1
[EMAIL PROTECTED] ~]$ cat out.1
This is stderr
This is stdout
[EMAIL PROTECTED] ~]$ ./t.sh 2>&1 > out.2
This is stderr ***(but printing to fd=1)
[EMAIL PROTECTED] ~]$ cat out.2
This is stdout
etc.etc..
- Rob
.