Bash assign output/error to variable

I have the following line in my bash file:

LIST=$(ssh 'ls -1 /web');

The problem I am having is that it is a part of automated script and I often get this on the stdout and not the data I need:

ssh_exchange_identification: Connection closed by remote host

I realize that LIST only gets the stdout of the ls. So I am looking for a command that would get more of the info from the commands. In particular:

  • stdout for ls – I have that right now
  • stderr for ls – not really interested, I don’t expect a problem there
  • stdout for ssh – Not interested, I don’t even know what it would output
  • stderr for sshTHIS IS WHAT I AM LOOKING FOR to check whether it ssh correctly. This being empty should mean that I have the data in $LIST I expect

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

From ssh man page on Ubuntu 16.04 (LTS):

     ssh exits with the exit status of the remote command or with 255 if an error occurred.

Knowing that, we can check exit status of ssh command. If exit status was 225, we know that it’s an ssh error, and if it’s any other non-zero value – that’s ls error.


TEST=$(ssh [email protected] 'ls /proc' 2>&1)

if [ $? -eq 0 ];
    printf "%s\n" "SSH command successful"
elif [ $? -eq 225   ]
    printf "%s\n%s" "SSH failed with following error:" "$TEST"
    printf "%s\n%s" "ls command failed" "$TEST"

Solution 2

Redirect ssh‘s standard error to a file within the command substitution and then test to see whether the file is empty or not:

output="$( ssh server 'command' 2>ssh.err )"

if [[ -s ssh.err ]]; then
    echo 'SSH error:' >&2
    cat ssh.err >&2

rm -f ssh.err

which displays SSH error: followed by the captured error output from ssh.

Solution 3

As I remember some variant must work. Try this:

cmd 2>>>$errmsg

triple redirect possibly… It was many years ago…
in variable errmsg must be output error message. Maybe I am wrong. In this case this variant get message 100% guarantee:

errmsg=`cmd 2>/dev/null`

This variant suppress error message while need it to out or may be not need. But usual output it catch also. So you may distinct those two cases by $? value and correct your script.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply