Java Reference
In-Depth Information
We can redirect its output to another location, a file, with the “>”
character:
$ ls > my.files
$
The output from the ls command no longer appears on the screen (the
default location of standard out); it has been redirected to the file my.files .
What makes this so powerful a construct (albeit for a very simple example)
is the fact that not only was no change to the program required, but the pro-
grammer who wrote the ls program also did nothing special for I/O. He simply
built the program to write to standard out. The shell did the work of redirecting
the output. This means that any program invoked by the shell can have its
output similarly redirected.
Standard error is another location for output, but it was meant as the des-
tination for error messages. For example, if you try to list the contents of a
nonexistent directory, you get an error message:
$ ls bogus
ls: bogus: No such file or directory
$
If you redirect standard out, nothing changes:
$ ls bogus > save.out
ls: bogus: No such file or directory
$
That's because the programmer wrote the program to send the message to
standard error, not standard out. In the shell ( bash ) we can redirect standard
error by preceding the redirect symbol with the number 2, as follows: 3
$ ls bogus 2> save.out
$
3. The use of the number 2 comes from an implementation detail: All the I/O descriptors for
a UNIX process were kept in an array. The first three elements of the array, numbered 0, 1,
and 2, were defined to be the standard in, out, and err, in that order. Thus in the shell you can
also redirect standard out by using “1>” as well as the shorter “>”.
Search WWH ::




Custom Search