Streams, pipes, redirection tutorial [was Re: OT: Tar]

Simon Bowring pmmail@rpglink.com
Thu, 24 Aug 2000 21:48:29 +0100 (BST)


>I couldn't get 'tar --help'
>to pipe to more.exe or less.exe, couldn't get it to output to a file,

Many unix (derived) utils output their help to stderr (the standard 
error output) rather than stdout (standard output) so that the help
is readable when utils are used incorrectly when strung together 
in pipelines etc.

stdout is "stream 1" and stderr is "stream 2"

When you pipe with "¦" this only redirects stream 1/stdout
Also ">" only redirects stream 1 /stdout

">" is a shorthand for the more general form "n>" where n is the stream
number, so   "dir > abc" is short for "dir 1> abc"

So, to redirect stderr you use "2>"
e.g.
   tar --help 2> filename.out

Also "&n" means the actual stream that stream "n" has been redirected to,
so to redirect stream 2 (stderr) to whereever stream 1 (stdout) is
going use:

   tar --help 2>&1  [Stderr redirected to stdout]

so you can:

   tar --help 2>&1 ¦ more [Stderr redirected to stdout, stdout 
                           piped into more]

Also
   tar --help >filename.out 2>&1 

which redirect stdout to filename.out and redirects stderr to the 
stream referenced by stream 1 (stdout) which is going to filename.out

"Obvious" really!! ;-)

Simon