Search code examples
bashoutputstderrio-redirectionopenssh

How to echo a variable following a string in bash?


I need to print the output of the "ssh -V" command inbetween some xml nodes. I want it to look like

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

but the code below is printing the command's output before the nodes.

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

Result:

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

Why is that happening? and how can I get around it?


Solution

  • Redirect ssh's stderr (2) to stdout (1), too:

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

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