在 bash 中,有没有办法回显/打印最后一个标准输出?是否有分配给 stdout 的变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6934848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In bash, is there a way to echo/print the last stdout? Is there a variable that stdout is assigned to?
提问by readdit
In bash, is there a way to echo/print the last stdout? Is there a variable that stdout is assigned to?
在 bash 中,有没有办法回显/打印最后一个标准输出?是否有分配给 stdout 的变量?
I don't want to redirect the output. I just want to be able to read/print it after a command is run.
我不想重定向输出。我只想在命令运行后能够读取/打印它。
回答by
Nope, there's no way to see a line sent to stdoutunless stdout's already been sent somewhere. If it was sent to a console, copy the text from that console. If you sent it to a file, tail -n 1that file. If you can re-run the command which generates the line you want to see, I would suggest piping it to tail -n 1to see just the last line of output.
不,stdout除非stdout已经发送到某处,否则无法看到发送到的行。如果它被发送到控制台,请从该控制台复制文本。如果你把它发送到一个文件,tail -n 1那个文件。如果您可以重新运行生成您想要查看的行的命令,我建议将其管道化tail -n 1以查看输出的最后一行。
Bash keeps a history of executed commands (~/.bash_historyby default in GNU Bash 4.2), but not of output.
Bash 保留已执行命令的历史记录(~/.bash_history默认情况下在 GNU Bash 4.2 中),但不保留输出。
回答by Mark Edgar
Try adding |tee outputto the end of your command.
尝试添加|tee output到命令的末尾。
If you want to capture the terminal output of your entire session, try script(1).
如果要捕获整个会话的终端输出,请尝试script(1)。
Also, this questioneris looking for the ability to search command output, so check answers there too.
此外,这个提问者正在寻找搜索命令输出的能力,所以也请检查那里的答案。

