简体   繁体   English

为什么“bash 2>out.txt”会隐藏 bash output?

[英]Why does "bash 2>out.txt" hide bash output?

I'm currently using zsh, and I'm trying to start bash from zsh so that all commands ran in this bash would have their stderr redirected to out.txt .我目前正在使用 zsh,并且正在尝试从 zsh 启动bash以便在此bash中运行的所有命令都将其 stderr 重定向到out.txt

bash 2>out.txt surely did the job, but normal bash output ( stdout ?) disappeared. bash 2>out.txt确实完成了这项工作,但正常的 bash output ( stdout ?)消失了。

What I expect:我的期望:

zsh> bash 2>out.txt
user@pc:~$ pwd
/home/user

What I got:我得到了什么:

pwd
/home/user

The user@pc:~$ thing has disappeared. user@pc:~$东西已经消失了。

Why is this happening?为什么会这样? I thought user@pc:~$ was supposed to be printed to stdout , but I tried redirecting stdout to /dev/null and it was still showing.我认为user@pc:~$应该打印到stdout ,但我尝试将stdout重定向到/dev/null并且它仍然显示。 It was also not showing in out.txt either, which means it isn't printed to stderr ?它也没有显示在out.txt中,这意味着它没有打印到stderr Then why does 2>out.txt make it disappear?那为什么2>out.txt让它消失呢?

Bash only prints the prompt in interactive mode; Bash 仅在交互模式下打印提示; redirecting its stderr to a non-terminal takes it out of that mode.将其标准错误重定向到非终端使其脱离该模式。 You can force interactive mode with -i , and if you do you'll notice that the prompt is in fact printed to stderr, not stdout.您可以使用-i强制交互模式,如果这样做,您会注意到提示实际上打印到 stderr,而不是 stdout。

But in general, if you want to capture an interactive session, you should use the script program (eg script -c bash out.txt on Linux, script out.txt bash on BSD/macOS). But in general, if you want to capture an interactive session, you should use the script program (eg script -c bash out.txt on Linux, script out.txt bash on BSD/macOS).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM