I’ve been struggling with this problem when writing a bash script.
Basically, I want to measure the time of a program on a remote server, so I use the command:
/usr/bin/time -f %e sh -c "my command > /dev/null 2>&1" to execute the program.
However, it appears that I cannot capture the output of my command (SSH) to a variable at all. In fact, the result (time) keeps getting printed out to stdout.
The full code is:
respond=$(ssh ${fromNode} /usr/bin/time "-f" "%e" "'sh' '-c' 'virsh migrate --live ${VM} qemu+ssh://${toNode}/system --verbose > /dev/null 2>&1'")
The value of respond is just empty, though the time is printed out to the standard output.
“time” command prints result to stderr, not to stdout. Thus it is not piped into your variable.
You should reroute stderr to stdout to achieve what you want:
And your full code can look something like this: