在 Bash 中,当它已经重定向时如何打印到标准输出?

In Bash, how to print to stdout when it’s already redirected?

我正在编写一个 Bash 函数,比如说 func() { … },它会交互式地询问用户几个问题,然后运行某个命令。

这里的奖励是上述命令的 stdout,我希望用户像这样调用我的函数:func >outfile,以在文件中捕获命令的输出。

我的问题是,如何在不污染有用输出的情况下将这些交互式问题打印到 stdout

换句话说:在一个函数中,如果 stdout 已被调用者潜在地重定向,我如何写入“原始” stdout (终端),暂时忽略调用者的重定向?

对于语义上不属于那里的输出,我是否必须求助于使用 stderr

回答原始问题

在定义函数的时间点创建原始标准输出的备份,您可以在调用时使用它。

exec {myfunc_stdout_fd}>&1
myfunc() {
  echo "Content sent to stdout as defined at invocation time"
  echo "Content sent to original stdout" >&"$myfunc_stdout_fd";
}

...此后:

myfunc_out=$(myfunc)

...将Content sent to stdout as defined at invocation time存储在myfunc_out中,并立即将Content sent to original stdout写入函数定义时定义的stdout。

https://ideone.com/HwHRJ7

的在线解释器中查看此 运行

关于最佳实践用法的建议

在 UNIX 上,提示通常写入 stderr,因此出于与提示相关的目的,通常不需要保留原始的 stdout。在 stderr 上提示是 read -p 所做的; bash 本身做了什么(和其他 shell 一样);等。这是适当的,因为 POSIX 将 stderr 定义为 "diagnostic output" 的适当流,这是一个类别,其中包括有关程序当前正在执行的操作的状态(是否准备好接受更多输入,f/e).

另请参阅:

  • How to display a custom prompt during the execution of C program?
  • What are the conventions for stdout/stderr messages?