| 名称 | 英文 | 文件描述符 | 操作符 |
|---|---|---|---|
| 标准输入 | stdin | 0 | < 或 << |
| 标准输出 | stdout | 1 | 1> 或 1>> (可简化为 > 或 >>) |
| 标准错误输出 | stderr | 2 | 2> 或 2>> |
echo helloworld > 1.txt 等同于 echo helloworld 1> 1.txthelloworld 输入进 1.txt 文件中echo helloworld >> 1.txt 等同于 echo helloworld 1>> 1.txthelloworld 追加到 1.txt 最后一行echoerr 2> 1.txt1.txt 文件中echo helloworld > 1.txt 2>&1,这里面我们可以拆分几个关键点:echo helloworld、> 1.txt、2>&1
拆分来看:
echo helloworld:一个普通的 echo 命令,没啥好讲的> 1.txt 或 1> 1.txt: 重定向文件描述符为 1 (stdout)的输出流地址2>&1 是什么意思?&1 可以理解为取地址符,取 stuout 的重定向输出流的地址;2> 可以理解为将 stderr 输出到 stdout 定向的文件地址。所以得出结论:
本来输出到控制台显示的 helloworld 会被输出到 1.txt 文件中,如果 echo 发生错误,会讲异常信息也一起输出到 1.txt 文件中
&>
如上若我们像简写 > 1.txt 2>&1,我们可以直接使用 &>
/dev/null
指向一个不存在的文件
&> /dev/null 表示什么信息也不输出