Sophie

Sophie

distrib > Mandriva > 8.1 > i586 > by-pkgid > 628e26a49117deea42e952d5b0d0f0d7 > files > 69

zsh-doc-4.0.2-2mdk.i586.rpm

<HTML>
<HEAD>
<!-- Created by texi2html 1.56k from ./zsh.texi on 26 June 2001 -->

<TITLE>zsh - Redirection</TITLE>
</HEAD>
<BODY>
Go to the <A HREF="zsh_1.html">first</A>, <A HREF="zsh_5.html">previous</A>, <A HREF="zsh_7.html">next</A>, <A HREF="zsh_29.html">last</A> section, <A HREF="zsh_toc.html">table of contents</A>.
<P><HR><P>


<H1><A NAME="SEC27" HREF="zsh_toc.html#TOC27">Redirection</A></H1>
<P>
<A NAME="IDX70"></A>
<A NAME="IDX71"></A>
<A NAME="IDX72"></A>
If a command is followed by <TT>&#38;</TT>
and job control is not active,
then the default standard input
for the command is the empty file <TT>/dev/null</TT>.
Otherwise, the environment for the execution of a command contains the
file descriptors of the invoking shell as modified by
input/output specifications.


<P>
The following may appear anywhere in a simple command
or may precede or follow a complex command.
Expansion occurs before <VAR>word</VAR> or <VAR>digit</VAR>
is used except as noted below.
If the result of substitution on <VAR>word</VAR>
produces more than one filename,
redirection occurs for each
separate filename in turn.


<P>
<DL COMPACT>

<DT><TT>&#60;</TT> <VAR>word</VAR>
<DD>
Open file <VAR>word</VAR> for reading as standard input.

<DT><TT>&#60;&#62;</TT> <VAR>word</VAR>
<DD>
Open file <VAR>word</VAR> for reading and writing as standard input.
If the file does not exist then it is created.

<DT><TT>&#62;</TT> <VAR>word</VAR>
<DD>
Open file <VAR>word</VAR> for writing as standard output.
If the file does not exist then it is created.
If the file exists, and the <TT>CLOBBER</TT> option is unset,
this causes an error;
otherwise, it is truncated to zero length.

<DT><TT>&#62;|</TT> <VAR>word</VAR>
<DD>
<DT><TT>&#62;!</TT> <VAR>word</VAR>
<DD>
Same as <TT>&#62;</TT>, except that the file is truncated to zero length
if it exists, even if <TT>CLOBBER</TT> is unset.

<DT><TT>&#62;&#62;</TT> <VAR>word</VAR>
<DD>
Open file <VAR>word</VAR> for writing in append mode as standard output.
If the file does not exist, and the <TT>CLOBBER</TT>
option is unset, this causes an error;
otherwise, the file is created.

<DT><TT>&#62;&#62;|</TT> <VAR>word</VAR>
<DD>
<DT><TT>&#62;&#62;!</TT> <VAR>word</VAR>
<DD>
Same as <TT>&#62;&#62;</TT>, except that the file is created if it does not
exist, even if <TT>CLOBBER</TT> is unset.

<DT><TT>&#60;&#60;</TT>[<TT>-</TT>] <VAR>word</VAR>
<DD>
The shell input is read up to a line that is the same as
<VAR>word</VAR>, or to an end-of-file.
No parameter expansion, command substitution or
filename generation is performed on <VAR>word</VAR>.
The resulting document, called a
<EM>here-document</EM>, becomes the standard input.

If any character of <VAR>word</VAR> is quoted with
single or double quotes or a `<TT>\</TT>',
no interpretation is placed upon the characters of the document.
Otherwise, parameter and command substitution
occurs, `<TT>\</TT>' followed by a newline is removed,
and `<TT>\</TT>' must be used to quote the characters
`<TT>\</TT>', `<TT>$</TT>', `<TT>`</TT>' and the first character of <VAR>word</VAR>.

If <TT>&#60;&#60;-</TT> is used, then all leading
tabs are stripped from <VAR>word</VAR> and from the document.

<DT><TT>&#60;&#60;&#60;</TT> <VAR>word</VAR>
<DD>
Perform shell expansion on <VAR>word</VAR> and pass the result
to standard input.  This is known as a <EM>here-string</EM>.

<DT><TT>&#60;&#38;</TT> <VAR>number</VAR>
<DD>
<DT><TT>&#62;&#38;</TT> <VAR>number</VAR>
<DD>
The standard input/output is duplicated from file descriptor
<VAR>number</VAR> (see man page dup2(2)).

<DT><TT>&#60;&#38; -</TT>
<DD>
<DT><TT>&#62;&#38; -</TT>
<DD>
Close the standard input/output.

<DT><TT>&#60;&#38; p</TT>
<DD>
<DT><TT>&#62;&#38; p</TT>
<DD>
The input/output from/to the coprocess is moved to the standard input/output.

<DT><TT>&#62;&#38;</TT> <VAR>word</VAR>
<DD>
<DT><TT>&#38;&#62;</TT> <VAR>word</VAR>
<DD>
(Except where `<TT>&#62;&#38;</TT> <VAR>word</VAR>' matches one of the above syntaxes;
`<TT>&#38;&#62;</TT>' can always be used to avoid this ambiguity.)
Redirects both standard output and standard error (file descriptor 2)
in the manner of `<TT>&#62;</TT> <VAR>word</VAR>'.
Note that this does <EM>not</EM> have the same effect as `<TT>&#62;</TT> <VAR>word</VAR> <TT>2&#62;&#38;1</TT>'
in the presence of multios (see the section below).

<DT><TT>&#62;&#38;|</TT> <VAR>word</VAR>
<DD>
<DT><TT>&#62;&#38;!</TT> <VAR>word</VAR>
<DD>
<DT><TT>&#38;&#62;|</TT> <VAR>word</VAR>
<DD>
<DT><TT>&#38;&#62;!</TT> <VAR>word</VAR>
<DD>
Redirects both standard output and standard error (file descriptor 2)
in the manner of `<TT>&#62;|</TT> <VAR>word</VAR>'.

<DT><TT>&#62;&#62;&#38;</TT> <VAR>word</VAR>
<DD>
<DT><TT>&#38;&#62;&#62;</TT> <VAR>word</VAR>
<DD>
Redirects both standard output and standard error (file descriptor 2)
in the manner of `<TT>&#62;&#62;</TT> <VAR>word</VAR>'.

<DT><TT>&#62;&#62;&#38;|</TT> <VAR>word</VAR>
<DD>
<DT><TT>&#62;&#62;&#38;!</TT> <VAR>word</VAR>
<DD>
<DT><TT>&#38;&#62;&#62;|</TT> <VAR>word</VAR>
<DD>
<DT><TT>&#38;&#62;&#62;!</TT> <VAR>word</VAR>
<DD>
Redirects both standard output and standard error (file descriptor 2)
in the manner of `<TT>&#62;&#62;|</TT> <VAR>word</VAR>'.

</DL>

<P>
If one of the above is preceded by a digit, then the file
descriptor referred to is that specified by the digit
instead of the default 0 or 1.
The order in which redirections are specified is significant.
The shell evaluates each redirection in terms of the
(<EM>file descriptor</EM>, <EM>file</EM>)
association at the time of evaluation.
For example:


<P>

<BLOCKQUOTE>
<P>
... <TT>1&#62;</TT><VAR>fname</VAR> <TT>2&#62;&#38;1</TT>
</BLOCKQUOTE>

<P>
first associates file descriptor 1 with file <VAR>fname</VAR>.
It then associates file descriptor 2 with the file associated with file
descriptor 1 (that is, <VAR>fname</VAR>).
If the order of redirections were reversed,
file descriptor 2 would be associated
with the terminal (assuming file descriptor 1 had been)
and then file descriptor 1 would be associated with file <VAR>fname</VAR>.




<H2><A NAME="SEC28" HREF="zsh_toc.html#TOC28">Multios</A></H2>
<P>
<A NAME="IDX73"></A>
If the user tries to open a file descriptor for writing more than once,
the shell opens the file descriptor as a pipe to a process that copies
its input to all the specified outputs, similar to <CITE>tee</CITE>,
provided the <TT>MULTIOS</TT> option is set, as it is by default.  Thus:


<P>

<PRE>
date &#62;foo &#62;bar
</PRE>

<P>
writes the date to two files, named `<TT>foo</TT>' and `<TT>bar</TT>'.
Note that a pipe is an implicit redirection; thus


<P>

<PRE>
date &#62;foo | cat
</PRE>

<P>
writes the date to the file `<TT>foo</TT>', and also pipes it to cat.


<P>
If the <TT>MULTIOS</TT>
option is set, the word after a redirection operator is also subjected
to filename generation (globbing).  Thus


<P>

<PRE>
: &#62; *
</PRE>

<P>
will truncate all files in the current directory,
assuming there's at least one.  (Without the <TT>MULTIOS</TT>
option, it would create an empty file called `<TT>*</TT>'.)
Similarly, you can do


<P>

<PRE>
echo exit 0 &#62;&#62; *.sh
</PRE>

<P>
If the user tries to open a file descriptor for reading more than once,
the shell opens the file descriptor as a pipe to a process that copies
all the specified inputs to its output in the order
specified, similar to <CITE>cat</CITE>,
provided the <TT>MULTIOS</TT> option is set.  Thus


<P>

<PRE>
sort &#60;foo &#60;fubar
</PRE>

<P>
or even


<P>

<PRE>
sort &#60;f{oo,ubar}
</PRE>

<P>
is equivalent to `<TT>cat foo fubar | sort</TT>'.


<P>
Note that a pipe is an implicit redirection; thus


<P>

<PRE>
cat bar | sort &#60;foo
</PRE>

<P>
is equivalent to `<TT>cat bar foo | sort</TT>' (note the order of the inputs).


<P>
If the <TT>MULTIOS</TT> option is <EM>un</EM>set,
each redirection replaces the previous redirection for that file descriptor.
However, all files redirected to are actually opened, so


<P>

<PRE>
echo foo &#62; bar &#62; baz
</PRE>

<P>
when <TT>MULTIOS</TT> is unset will truncate bar, and write `<TT>foo</TT>' into baz.


<P>




<H2><A NAME="SEC29" HREF="zsh_toc.html#TOC29">Redirections with no command</A></H2>
<P>
<A NAME="IDX74"></A>
<A NAME="IDX75"></A>
<A NAME="IDX76"></A>
<A NAME="IDX77"></A>
When a simple command consists of one or more redirection operators
and zero or more parameter assignments, but no command name, zsh can
behave in several ways.


<P>
If the parameter <TT>NULLCMD</TT> is not set or the option <TT>CSH_NULLCMD</TT> is
set, an error is caused.  This is the <CITE>csh</CITE> behavior and <TT>CSH_NULLCMD</TT>
is set by default when emulating <CITE>csh</CITE>.


<P>
If the option <TT>SH_NULLCMD</TT> is set, the builtin `<TT>:</TT>' is inserted as a
command with the given redirections.  This is the default when emulating
<CITE>sh</CITE> or <CITE>ksh</CITE>.


<P>
Otherwise, if the parameter <TT>NULLCMD</TT> is set, its value will be used as a
command with the given redirections.  If both <TT>NULLCMD</TT> and
<TT>READNULLCMD</TT> are set, then the value of the latter will be used instead
of that of the former when the redirection is an input.  The default for
<TT>NULLCMD</TT> is `<TT>cat</TT>' and for <TT>READNULLCMD</TT> is `<TT>more</TT>'. Thus


<P>

<PRE>
&#60; file
</PRE>

<P>
shows the contents of <TT>file</TT> on standard output, with paging if that is a
terminal.  <TT>NULLCMD</TT> and <TT>READNULLCMD</TT> may refer to shell functions.


<P>


<P><HR><P>
Go to the <A HREF="zsh_1.html">first</A>, <A HREF="zsh_5.html">previous</A>, <A HREF="zsh_7.html">next</A>, <A HREF="zsh_29.html">last</A> section, <A HREF="zsh_toc.html">table of contents</A>.
</BODY>
</HTML>