Linux Shell 标准输出和标准错误输出指定的先后顺序及影响

# 先将标准错误输出2 指向标准输出1,

# 而此时标准输出1 指向的是控制台,所以标准错误输出2 指向的也是控制台,

# 之后,将标准输出1 指向文件/tmp/t.txt,但标准错误输出2 仍指向控制台。

ls non_exists 2>&1 > /tmp/t.txt # 所以,错误信息打印到了屏幕上,

ls: cannot access non_exists: No such file or directory

cat /tmp/t.txt # 而/tmp/t.txt 里却没有内容。

# 先将标准输出1 指向文件/tmp/t.txt,

# 再将标准错误输出2 指向标准输出1,

# 这样标准错误输出2 和标准输出1 都指向了文件/tmp/t.txt

ls non_exists > /tmp/t.txt 2>&1 # 屏幕上不再出现错误信息,

扫描二维码关注公众号,回复: 10886836 查看本文章

cat /tmp/t.txt # 错误信息都保存到了文件/tmp/t.txt 里

ls: cannot access non_exists: No such file or directory

发布了27 篇原创文章 · 获赞 4 · 访问量 9690

猜你喜欢

转载自blog.csdn.net/yoshubom/article/details/104630299