Let's examine my problem in layers:
sleep 3; echo hello
sleeps for three seconds and echoes "hello"
ssh localhost "sleep3; echo hello"
does the same on a remote host over ssh
var=$(ssh localhost "sleep3; echo hello")
does the same but stores "hello" in var
(time var=$(ssh localhost "sleep3; echo hello")) 2>&1
does the same and times it, but since time outputs to stderr, I redirected stderr to stdout, so that I can do this
timer=$((time var=$(ssh localhost "sleep3; echo hello")) 2>&1)
which should make the remote machine wait for 3 seconds, echo "hello" which should be stored on a local machine's variable var
, and time the entire process after which the timed value is stored in variable timer
.
But unfortunately, var
is EMPTY (it works fine in example #3)! For the purpose of my code, it is necessary to store output of ssh in one variable, and ssh execution time in the other, without the use of "more advanced" commands as /bin/time
, date
, etc... I am sure there is a clever (i.e. shell-only) way of doing this, but I'm not clever enough to figure it out by my self. Any help is greatly appreciated.
Here's a thread that might be useful: [How to store standard error in a variable in a Bash script