Using docker command output as variable

I’m trying to automate a few things, including updates inside one of my containers. I figured it would be a good idea to use the output of a command as variable and if that variable contains a certain value, restart the container.

I created the following test:

update_nodejs=$( sudo docker exec homebridge hb-service update-node --allow-root )

if [[ $update_nodejs != *"already up-to-date"* ]]
then
  sudo docker restart homebridge
fi

My problem: where this works for commands like “git pull”, in this case the variable is/stays empty. I have also tried appending the output to a file, with the same result.

I’m running Docker on Synology, don’t know if that helps solving this issue?

The output is probbaly going to the standard error stream, not the standard output. You can redirect the error stream to the standard output by ending or starting the command with 2>/dev/stdout:

In your code:

update_nodejs=$( sudo docker exec homebridge hb-service update-node --allow-root 2>/dev/stdout )
1 Like

That works! Thanks!!