Redirect Terminal input and output on Mac
From the command line, you can redirect input and output from a command to a file or to another command.
Redirect output from the command if you want to capture the results of running the command and store it in a file for later use. Similarly, redirect input from a file to the command if you want to provide the command with preset input data, instead of having to enter that data.
Use the following characters to redirect input and output:
Redirect | Description | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
> | Use a right-angle bracket to redirect command output to a file. | ||||||||||
< | Use a left-angle bracket to use the contents of a file as input to the command. | ||||||||||
>> | Use two right-angle brackets to append output from a command to a file. |
In addition to using file redirection, you can also redirect the output of one command to the input of another using the vertical bar character or pipe. You can combine commands in this manner to implement more sophisticated versions of the same commands.
For example, the following command passes the formatted contents of the zsh
man page to the grep
tool, which searches the contents for lines containing the word commands. The result is a list of lines with the specified text, instead of the entire man page.
% man zsh | grep commands
Standard pipes include:
stdin: The standard input pipe is where a command receives input. By default, you enter input from the command-line interface. You can redirect the output from files or other commands to stdin.
stdout: The standard output pipe is where command output is sent. By default, command output is sent to the command line. You can redirect the output from the command line to other commands and tools.
stderr: The standard error pipe is where error messages are sent. By default, errors are displayed on the command line along with standard output.
For more information about redirection, see the zsh man page.