Redirection and Process Substitution

Both the input and output of a command may be redirected:

command  >  filename    Redirect command output into a file

command  >> filename    Redirect command output and APPEND into a file

command  <  filename    Redirect a file into a command

command  |  filename    Redirect output from one command into another

command | tee filename | commandB    Redirect command into filename AND CommandB

       Builtin	commands are executed within the shell.	 If any component of a
       pipeline except the last is a builtin command, the pipeline is executed
       in a subshell.

       Parenthesized commands are always executed in a subshell.

	   (cd; pwd); pwd

       thus  prints  the  home directory, leaving you where you were (printing
       this after the home directory), while

	   cd; pwd

       leaves you in the home  directory.   Parenthesized  commands  are  most
       often used to prevent cd from affecting the current shell.

The NOCLOBBER option can prevent you from overwriting an existing file.

Redirecting Input/Output:

The standard input and standard output of a command may be redirected
with the following syntax:

       < name  Open file name (which is first variable, command	 and  filename
	       expanded) as the standard input.

       << word Read  the  shell input up to a line which is identical to word.
	       word is not subjected to variable, filename or command  substi-
	       tution, and each input line is compared to word before any sub-
	       stitutions are done on this input line.	Unless a quoting  `\',
	       `"',  `'	 or ``' appears in word variable and command substitu-
	       tion is performed on the intervening  lines,  allowing  `\'  to
	       quote  `$',  `\'	 and ``'.  Commands which are substituted have
	       all blanks, tabs, and newlines preserved, except for the	 final
	       newline	which  is dropped.  The resultant text is placed in an
	       anonymous temporary file which is given to the command as stan-
	       dard input.

       > name
       >! name
       >& name
       >&! name
	       The file name is used as standard output.  If the file does not
	       exist then it is created; if the file exists, it is  truncated,
	       its previous contents being lost.

	       If  the shell variable noclobber is set, then the file must not
	       exist or be a character	special	 file  (e.g.,  a  terminal  or
	       `/dev/null')  or an error results.  This helps prevent acciden-
	       tal destruction of files.  In this case the `!'	forms  can  be
	       used to suppress this check.

	       The  forms  involving  `&' route the diagnostic output into the
	       specified file  as  well	 as  the  standard  output.   name  is
	       expanded in the same way as `<' input filenames are.

       >> name
       >>& name
       >>! name
       >>&! name
	       Like  `>', but appends output to the end of name.  If the shell
	       variable noclobber is set, then it is an error for the file not

	       to exist, unless one of the `!' forms is given.

       A  command  receives  the environment in which the shell was invoked as
       modified by the input-output parameters and the presence of the command
       in  a pipeline.	Thus, unlike some previous shells, commands run from a
       file of shell commands have no access to the text of  the  commands  by
       default;	 rather they receive the original standard input of the shell.
       The `<<' mechanism should be used to present inline data.  This permits
       shell command scripts to function as components of pipelines and allows
       the shell to block read its input.   Note  that	the  default  standard
       input  for  a command run detached is not the empty file /dev/null, but
       the original standard input of the shell.  If this is a terminal and if
       the  process  attempts to read from the terminal, then the process will
       block and the user will be notified .

       Diagnostic output may be directed through a pipe with the standard out-
       put.  Simply use the form `|&' rather than just `|'.

       The  shell  cannot  presently  redirect	diagnostic output without also
       redirecting standard output, but 
           `(command  >  output-file)  >& error-file'  is often an acceptable
       workaround.  Either output-file or error-file may be `/dev/tty' to
       send output to the terminal.

Here Documents

This type of redirection instructs the shell to read input from the current source until a line containing only word (with no trailing blanks) is seen. All of the lines read up to that point are then used as the standard input for a command.

The format of here-documents is as follows:

<<[-]word
        here-document
delimiter

No parameter expansion, command substitution, arithmetic expansion, or filename expansion is performed on word. If any characters in word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion. In the latter case, the character sequence \newline is ignored, and `\' must be used to quote the characters `\', `$', and ``'.

Related:

OS X Syntax


© Copyright SS64.com 1999-2013
Some rights reserved