Both the input and output of a command can 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 commandA < (commandB) Redirect the output of commandB as file input to commandA commandA | tee filename | commandB Redirect commandA into filename AND commandB commandA | commandB Redirect stdout of commandA to commandB commandA |& commandB Redirect stdERR of commandA to commandB commandA & commandB Run commandA and then run commandB (asynchronous). commandA ; commandB Run commandA and afterwards run commandB (synchronous) commandA && commandB Run commandB only if commandA is successful (synchronous AND) commandA || commandB Run commandB only if commandA is NOT successful command & Run command in a subshell. If a command is terminated by the control operator &, the shell executes the command in the background 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:
>(command_list)
<(command_list)
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 delimiterNo 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 macOS commands:
Command Substitution
macOS Syntax