一条命令中有两个重定向运算符 [英] Two redirection operators in one command

查看:32
本文介绍了一条命令中有两个重定向运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请解释此shell命令的输出:

Please explain the output of this shell command:

ls> file1> file2

ls >file1 >file2

为什么输出转到文件2而不是文件1?

Why does the output go to file2 instead of file1?

推荐答案

bash 每个文件描述符仅允许一个重定向.如果提供了多个重定向(如您的示例中所示),则会从左到右进行处理,最后一个是唯一生效的重定向.(不过请注意,每个文件仍将被创建,或者如果已经存在则被截断;其他文件将不会被进程使用.)

bash only allows one redirection per file descriptor. If multiple redirections are provided, like in your example, they are processed from left to right, with the last one being the only one that takes effect. (Notice, though, that each file will still be created, or truncated if already in existence; the others just won't be used by the process.)

某些外壳程序(例如 zsh )具有允许多次重定向的选项.在 bash 中,您可以通过对 tee 的一系列调用来模拟这一点:

Some shells (like zsh) have an option to allow multiple redirections. In bash, you can simulate this with a series of calls to tee:

ls | tee file1 file2 > /dev/null

每次对 tee 的调用都会将其输入写入命名文件及其标准输出.

Each call to tee writes its input to the named file(s) and its standard output.

这篇关于一条命令中有两个重定向运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆