Linux 终端操作之「I/O Redirection」

I/O 重定向是在终端运行程序时很常用的技巧,但是我对它所知甚少。今天我在 DigitalOcean 上发现了一篇很好的 tutorial。这篇随笔记录一下我的心得体会和发现的一个问题。

  1. I/O redirection 是附在命令末尾的一个表达式(expression),严格说来并非命令的主体,只是修饰性的。
  2. I/O redirection 命令 >>><<< 是二元运算符(binary operator),其两侧的参数(也称「操作数」,operands)是文件名或文件描述符;如果显式地指定左侧参数,则参数和运算符之间不能有空格,否则该参数可能会被当作主命令的参数。

上面提到的那篇 tutorial 里面有一个地方描述有误

command 2> file

This pattern redirects the standard error stream of a command to a file, overwriting existing contents.

mkdir '' 2> mkdir_log.txt
This redirects the error raised by the invalid directory name '', and writes it to log.txt. Note that the error is still sent to the terminal and displayed as text.

这里所说的「the error is still sent to the terminal and displayed as text」与实际情况不符。

Reference

https://www.digitalocean.com/community/tutorials/an-introduction-to-linux-i-o-redirection

原文地址:https://www.cnblogs.com/Patt/p/7818498.html