如何重定向zsh / bash的标准输出并稍后还原

问题描述 投票:0回答:1

在python中,您可以

sys.stdout = open('log', 'w') # begin redirect

然后输出将改为写入log

您可以使用以下方法恢复到正常状态

sys.stdout = sys.__stdout__   # end redirect, restore back

如何在zsh和bash中实现类似的效果?

P.S。,

  • 也应该重定向子shell中的命令stdout。
  • ls > log不是我想要的。

为了澄清,我想要的是

ls   # output to terminal
# begin redirect to `log`
ls   # output to `log`
find -type f   # output to `log`
...  # output to `log`
# end redirect, restore back
ls   # output to terminal
bash zsh io-redirection
1个回答
0
投票

您可以使用tee命令登录到文件以及打印到控制台:

ls                         # output to terminal
# begin redirect to `log`
ls  | tee -a log           # output to `log`
find -type f | tee -a log  # output to `log`
...  # output to `log`
# end redirect, restore back
ls   # output to terminal
© www.soinside.com 2019 - 2024. All rights reserved.