help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Redirecting both stdout and stdin to a file


From: Bob Proulx
Subject: Re: [Help-bash] Redirecting both stdout and stdin to a file
Date: Sat, 28 Jan 2012 17:09:09 -0700
User-agent: Mutt/1.5.21 (2010-09-15)

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.

Bob



reply via email to

[Prev in Thread] Current Thread [Next in Thread]