Both the input and output of a command may be redirected:
command > filename Redirect command output (stdout) into a file command > /dev/null Discard stdout of command command 2> filename Redirect error output (stderr) to a file command 2>&1 filename Redirect stderr to stdout command 1>&2 filename Redirect stdout to stderr command >> filename Redirect command output and APPEND into a file command < filename Redirect a file into a command command1 < (command2) Redirect the output of command2 as file input to command1 command1 | tee filename | command2 Redirect command1 into filename AND command2 command1 | command2 Redirect stdout of command1 to command2 command1 |& command2 Redirect stdERR of command1 to command2 command1 ; command2 Run command1 and then command2 command1 && command2 Run command2 only if command1 is successful command1 || command2 Run command2 only if command1 is NOT successful command & Run command in a subshell command &> filename Redirect every output of command to filename
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.
Piping the stdout of a command into the stdin of another is a powerful technique. But, what if you need to pipe the stdout of multiple commands? This is where process substitution comes in. Process substitution feeds the output of a process (or processes) into the stdin of another process.
Template - command list enclosed within parentheses:
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 substitution 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 output. 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.
In all the descriptions above, if the file descriptor number is omitted, and the first character of the redirection operator is `<', the redirection refers to the standard input (file descriptor 0). If the first character of the redirection operator is `>', the redirection refers to the standard output (file descriptor 1).
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
\newlineis ignored, and \ must be used to quote the characters \, $, and `.
OS X Syntax