[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Redirecting both stdout and stdin to a file
From: |
Peng Yu |
Subject: |
Re: [Help-bash] Redirecting both stdout and stdin to a file |
Date: |
Sat, 28 Jan 2012 18:25:08 -0600 |
On Sat, Jan 28, 2012 at 6:09 PM, Bob Proulx <address@hidden> wrote:
> Peng Yu wrote:
>> There must be some rule that I missed. Could anybody let me know how
>> to understand the difference between the two commands?
>
> The shell holds the file descriptors in program variables. These
> variables are assigned in the order seen on the command line from left
> to right. The following pseudo code isn't from the actual shell code
> but perhaps thinking of it this way will help model it for you.
>
> ">FILE 2>&1" # Redirects both stdout to FILE and then redirects
> # stderr to the current stdout.
> 1>FILE <-- fd[1] = open_for_reading(FILE)
> 2>&1 <-- fd[2] = fd[1]
>
> "2>&1 >FILE" # Redirects stderr to the current stdout and then
> # redirects stdout to file.
> 2>&1 <-- fd[2] = fd[1]
> 1>FILE <-- fd[1] = open_for_reading(FILE)
>
> In the second case it should now be apparent that the stderr isn't
> associated with FILE because of the ordering.
I didn't understand that this is like file handle assignment under the
surface. Now I understand how this works.
BTW, it seems that the word "redirection" is little bit misleading.
For example, I original thought that the original stdout is redirected
to FILE and the orignal stderr is redirected to the new stdout. That
is why I thought FILE should got the original stdout and what was in
stderr get print on the screen. A better term should be something
"file handle assignment".
">FILE 2>&1"
I reread the Redirection section in the man page. I don't think that
the description there help me understand the two cases that I
mentioned in the first email. Please let me know which lines explain
them, if anybody think that the redirection section explains the two
cases. Otherwise, I suggest to put Bob's explanation in the manual to
clarify how redirection works (at least as a mental model, even it is
not exact what bash does internally).
--
Regards,
Peng