如何在bash中的字符串后回显变量?

问题描述

我需要在一些xml节点之间打印“ ssh -V”命令的输出。我希望它看起来像

<sshname>OpenSSH_7.4p1,OpenSSL 1.0.2k-fips  26 Jan 2017</sshname>

但是下面的代码在节点之前打印了命令的输出

sshname=$(ssh -V)
echo "<sshname> $sshname</sshname>"

结果:

OpenSSH_7.4p1,OpenSSL 1.0.2k-fips  26 Jan 2017
<sshname> </sshname>

为什么会这样?以及如何解决呢?

解决方法

也将ssh的stderr(2)重定向到stdout(1):

sshname=$(ssh -V 2>&1)

请参阅:https://en.wikipedia.org/wiki/Standard_streams