【问题标题】:How to echo a variable following a string in bash? [duplicate]如何在bash中的字符串后面回显一个变量? [复制]
【发布时间】:2021-01-27 15:18:29
【问题描述】:

我需要在一些 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>

为什么会这样?我该如何解决?

【问题讨论】:

    标签: bash output stderr io-redirection openssh


    【解决方案1】:

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

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

    见:https://en.wikipedia.org/wiki/Standard_streams

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-18
      • 2018-09-18
      • 2017-10-29
      • 2021-11-22
      • 2011-05-14
      • 1970-01-01
      相关资源
      最近更新 更多