Allen Hu
Allen Hu

Reputation: 655

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.

bash 2>out.txt surely did the job, but normal bash output (stdout?) disappeared.

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.

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. It was also not showing in out.txt either, which means it isn't printed to stderr? Then why does 2>out.txt make it disappear?

Upvotes: 1

Views: 58

Answers (1)

Mark Reed
Mark Reed

Reputation: 95267

Bash only prints the prompt in interactive mode; 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.

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

Upvotes: 1

Related Questions