Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 22 May 1998 17:06:16 +0930
From:      Greg Lehey <grog@lemis.com>
To:        flygt@sr.se, freebsd-questions@FreeBSD.ORG
Subject:   Re: Newbie question
Message-ID:  <19980522170616.A334@freebie.lemis.com>
In-Reply-To: <19980522091434.12772@sr.se>; from Gunnar Flygt on Fri, May 22, 1998 at 09:14:34AM %2B0200
References:  <19980522091434.12772@sr.se>

next in thread | previous in thread | raw e-mail | index | archive | help
On Fri, 22 May 1998 at  9:14:34 +0200, Gunnar Flygt wrote:
> What does the ´>/dev/null 2>&1' seen in many compilations actually do? I
> understand that the output is redirected to null but the rest does what?

By default, all programs started by a shell (and that's most programs
you'll start) inherit three open files:

0:  standard input (stdin)
1:  standard output (stdout)
2:  standard error (stderr)

Normally, the program will write normal output to stdout, and error
messages to stderr.  This enables you to send them to different
places.

You can redirect all of these streams elsewhere.  To make the file
/foo/bar your stdin, you write:

  prog < /foo/bar

To make /foo/baz your output file, you write:

  prog > /foo/baz

To make /foo/zot your error file, you write:

  prog 2>/foo/zot

The 2 there is the file descriptor number.  You can open other files
this way:

  prog 5>/tmp/strangeone

will pass a file descriptor 5 to the program.

You can also divert one file descriptor to another.  In particular,
you may want to divert stderr (2) to stdout (1).  You do this in some
shells with the construct

  prog 2>&1

So

  prog >/dev/null

will write stdout to /dev/null, the system bit bucket.

  prog 2>/dev/null

will write stderr to the bit bucket.  You could write

  prog >/dev/null 2>/dev/null

to write both to the bit bucket, but it's easier to write:

  prog >/dev/null 2>&1

Note that the shell interpets the information from left to right.
This isn't the same thing:

  prog 2>&1 >/dev/null

This will write the stderr to the default stdout, and stdout to
/dev/null.

There's a lot more of this in "UNIX Power Tools", by O'Reilly.  I
heartily recommend it.

Greg
--
See complete headers for address and phone numbers
finger grog@lemis.com for PGP public key

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?19980522170616.A334>