[ale] what does >&2 mean in bash

Ed Cashin ecashin at noserose.net
Sat May 13 21:07:39 EDT 2017


It is surprisingly flexible.  Below I save the place file descriptor one is
going to by using file descriptor six.  (The convention in UNIX-like
systems is for file descriptor zero to be standard input, file descriptor
one to be standard output, and file descriptor two to be standard error.
For interactive shell sessions, all three are connected to the terminal at
first.)

Then I redirect standard output to a file named, "output", and write to it
just by doing a regular "echo" command.  When I try to cat the file, it
doesn't work, because it would be an infinite loop, I suppose!  So I put
standard output back by restoring the "backup" from file descriptor six.

In the example, I take advantage of the fact that you can redirect the
current shell's file descriptors by using "exec".

ecashin at montgomery:~/tmp$ sh
$ exec 6>&1
$ exec 1>output
$ echo hi there
$ cat output
cat: output: input file is output file
$ exec 1>&6
$ cat output
hi there
$

You can redirect input as well, but it has never been easy for me to
remember the syntax, and it didn't feel as useful and smooth an experience
to use that feature.


On Sat, May 13, 2017 at 3:49 PM, Byron Jeff <byronjeff at clayton.edu> wrote:

> On Sat, May 13, 2017 at 02:30:15PM -0400, Pete Hardie wrote:
> >    If memory serves, it means to redirect stderr to the same place a
> >    stdout.  Useful when you want all the output (normal and error) to go
> >    to the same file/pipe
>
> Sort of. The left hand side is the target while the right hand side is the
> source. Technically redirecting stderr to stdout is:
>
> 2>&1
>
> 2 is stderr and the source. So that redirects stderr to the same place as
> stdout. Narahari specified the opposite:
>
> echo ... >&2
>
> Here stdout is the source because it's the default, stderr is the target.
>
> In the end they both accomplish the same goal, which is getting both stdout
> and stderr to the same place. The difference is that often stdout is
> already redirected whereas typically stderr is generally attached to the
> screen. So the goal of the second is to ensure that the echo output goes to
> the screen even if stdout overall for the script is redirected to a file,
> whereas the first one guarantees that if stdout is already redirected, that
> the error messages also follow that redirection.
>
> BAJ
>
> >
> >    On Sat, May 13, 2017 at 2:11 PM, Narahari 'n' Savitha
> >    <[1]savithari at gmail.com> wrote:
> >
> >    Friends:
> >    I see this in a sample code
> > echo "[statsgen] restarting myself" >&2
> >
> >    What does the >&2 mean ?
> >    -N
> >
> >      _______________________________________________
> >      Ale mailing list
> >      [2]Ale at ale.org
> >      [3]http://mail.ale.org/mailman/listinfo/ale
> >      See JOBS, ANNOUNCE and SCHOOLS lists at
> >      [4]http://mail.ale.org/mailman/listinfo
> >
> >    --
> >    Pete Hardie
> >    --------
> >    Better Living Through Bitmaps
> >
> > References
> >
> >    1. mailto:savithari at gmail.com
> >    2. mailto:Ale at ale.org
> >    3. http://mail.ale.org/mailman/listinfo/ale
> >    4. http://mail.ale.org/mailman/listinfo
>
> > _______________________________________________
> > Ale mailing list
> > Ale at ale.org
> > http://mail.ale.org/mailman/listinfo/ale
> > See JOBS, ANNOUNCE and SCHOOLS lists at
> > http://mail.ale.org/mailman/listinfo
>
>
> --
> Byron A. Jeff
> Associate Professor: Department of Computer Science and Information
> Technology
> College of Information and Mathematical Sciences
> Clayton State University
> http://faculty.clayton.edu/bjeff
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo
>



-- 
  Ed Cashin <ecashin at noserose.net>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.ale.org/pipermail/ale/attachments/20170513/76584cbf/attachment.html>


More information about the Ale mailing list