title: Load output from bash time command to a variable
body: <p>Let's examine my problem in layers:</p>
  
  <pre><code>sleep 3; echo hello
  </code></pre>
  
  <p>sleeps for three seconds and echoes "hello"</p>
  
  <pre><code>ssh localhost "sleep3; echo hello"
  </code></pre>
  
  <p>does the same on a remote host over ssh</p>
  
  <pre><code>var=$(ssh localhost "sleep3; echo hello")
  </code></pre>
  
  <p>does the same but stores "hello" in <code>var</code></p>
  
  <pre><code>(time var=$(ssh localhost "sleep3; echo hello")) 2&gt;&amp;1
  </code></pre>
  
  <p>does the same and times it, but since time outputs to stderr, I redirected stderr to stdout, so that I can do this</p>
  
  <pre><code>timer=$((time var=$(ssh localhost "sleep3; echo hello")) 2&gt;&amp;1)
  </code></pre>
  
  <p>which should make the remote machine wait for 3 seconds, echo "hello" which should be stored on a local machine's variable <code>var</code>, and time the entire process after which the timed value is stored in variable <code>timer</code>.</p>
  
  <p>But unfortunately, <code>var</code> 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 <code>/bin/time</code>, <code>date</code>, 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.</p>
  
  <p>Here's a thread that might be useful: [<a href="https://stackoverflow.com/questions/962255/how-to-store-standard-error-in-a-variable-in-a-bash-script][1]">How to store standard error in a variable in a Bash script</a></p>