• Open Menu Close Menu
  • Apple
  • Apple
Terminal User Guide
打开菜单关闭菜单
  • 社区
  • 联系支持
macOS Catalina 10.15
macOS Mojave 10.14
macOS High Sierra
目录

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.

See alsoApple Support article: Use zsh as the default shell on your MacExecute commands and run tools in Terminal on MacSpecify files and folders in Terminal on MacApple Developer website: Command Line Primer
Apple
  1. 支持
  2. Terminal User Guide
  3. Redirect Terminal input and output on Mac
新加坡
Copyright © 2019 Apple Inc. 保留所有权利。
隐私政策 使用条款