1

This question already has an answer here:

Unable to assign the SSH version number to a variable in bash. Running the below code, the output from "uname -a" is assigned to the variable "os", however the output from "ssh -v" appears in the terminal window but isn't assigned to the variable "ssh_v".

Confirmed this is happening on various versions of Solaris and Ubuntu and I have no idea why.

CODE:

#!/usr/bin/env bash

ssh_v=$(ssh -V)
printf "ssh_version = $ssh_v\n"

os=$(uname -a)
printf "OS_version = $os\n"

OUTPUT:

root@myserver:/usr/build/run# ./test.sh
Sun_SSH_2.4, SSH protocols 1.5/2.0, OpenSSL 0x100020bf
ssh_version =
OS_version = SunOS myserver 5.11 11.3 sun4v sparc sun4v

marked as duplicate by Kusalananda bash Feb 5 at 6:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

It looks like SSH is printing the version in stderr, not stdout. So you need to redirect stderr to stdout:

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.