输出未捕获到bash变量中 [英] Output not captured in bash variable
问题描述
我注意到,当尝试将命令的输出捕获到bash变量时,尽管没有完成变量分配,但仍将某些行打印在了终端上,但根本没有捕获到这些行.启用标题消息时, ssh-keyscan
是一个示例.另一个示例是来自Python SimpleHTTPServer
的访问日志.
I noticed that when trying to capture output of a command to a bash variable, some lines are not captured at all, although they are printed on the terminal when variable assigning is not done. One example is ssh-keyscan
, when banner messages are enabled. Another example is access logs from a Python SimpleHTTPServer
.
这些如何在终端上打印但不捕获到变量中?
How are these getting printed on the terminal but not captured to a variable?
推荐答案
fedorqui的评论中可能有原因...
Probably fedorqui have a reason in his comment...
在这种情况下,您可以使用重定向选项 2>& 1
将stderr重定向到stdout.像这样:
And in this case you could redirect stderr to stdout using redirect option 2>&1
.
Like this :
toto=$( ls tioto 2>&1 )
echo $toto
ls: cannot access tioto: No such file or directory
这篇关于输出未捕获到bash变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!