<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>&</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><</TT> <VAR>word</VAR> <DD> Open file <VAR>word</VAR> for reading as standard input. <DT><TT><></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>></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>>|</TT> <VAR>word</VAR> <DD> <DT><TT>>!</TT> <VAR>word</VAR> <DD> Same as <TT>></TT>, except that the file is truncated to zero length if it exists, even if <TT>CLOBBER</TT> is unset. <DT><TT>>></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>>>|</TT> <VAR>word</VAR> <DD> <DT><TT>>>!</TT> <VAR>word</VAR> <DD> Same as <TT>>></TT>, except that the file is created if it does not exist, even if <TT>CLOBBER</TT> is unset. <DT><TT><<</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><<-</TT> is used, then all leading tabs are stripped from <VAR>word</VAR> and from the document. <DT><TT><<<</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><&</TT> <VAR>number</VAR> <DD> <DT><TT>>&</TT> <VAR>number</VAR> <DD> The standard input/output is duplicated from file descriptor <VAR>number</VAR> (see man page dup2(2)). <DT><TT><& -</TT> <DD> <DT><TT>>& -</TT> <DD> Close the standard input/output. <DT><TT><& p</TT> <DD> <DT><TT>>& p</TT> <DD> The input/output from/to the coprocess is moved to the standard input/output. <DT><TT>>&</TT> <VAR>word</VAR> <DD> <DT><TT>&></TT> <VAR>word</VAR> <DD> (Except where `<TT>>&</TT> <VAR>word</VAR>' matches one of the above syntaxes; `<TT>&></TT>' can always be used to avoid this ambiguity.) Redirects both standard output and standard error (file descriptor 2) in the manner of `<TT>></TT> <VAR>word</VAR>'. Note that this does <EM>not</EM> have the same effect as `<TT>></TT> <VAR>word</VAR> <TT>2>&1</TT>' in the presence of multios (see the section below). <DT><TT>>&|</TT> <VAR>word</VAR> <DD> <DT><TT>>&!</TT> <VAR>word</VAR> <DD> <DT><TT>&>|</TT> <VAR>word</VAR> <DD> <DT><TT>&>!</TT> <VAR>word</VAR> <DD> Redirects both standard output and standard error (file descriptor 2) in the manner of `<TT>>|</TT> <VAR>word</VAR>'. <DT><TT>>>&</TT> <VAR>word</VAR> <DD> <DT><TT>&>></TT> <VAR>word</VAR> <DD> Redirects both standard output and standard error (file descriptor 2) in the manner of `<TT>>></TT> <VAR>word</VAR>'. <DT><TT>>>&|</TT> <VAR>word</VAR> <DD> <DT><TT>>>&!</TT> <VAR>word</VAR> <DD> <DT><TT>&>>|</TT> <VAR>word</VAR> <DD> <DT><TT>&>>!</TT> <VAR>word</VAR> <DD> Redirects both standard output and standard error (file descriptor 2) in the manner of `<TT>>>|</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></TT><VAR>fname</VAR> <TT>2>&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 >foo >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 >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> : > * </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 >> *.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 <foo <fubar </PRE> <P> or even <P> <PRE> sort <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 <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 > bar > 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> < 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>