title: How to assign local variable with a remote command result in bash script?
body: <p>I'm writing a script to restore Master-Slave replication on a set of servers. Lost in bash syntax trying to assign a local variable with a result of a remotely ran command substitution with local values:</p>
  
  <pre><code>function doRemote() {
      ssh s1.domain.com   &lt;&lt;ENDSSH
          mysql -u root -pXXX --execute="DROP DATABASE db; CREATE DATABASE db;"
          mysql -u root -pXXX --database=db &lt; $WORKDIR$FILENAME
          sudo rm -rf /var/log/mysql/db-bin.*
          mysql -u root -pXXX --execute="FLUSH LOGS;"
          CURRENT_LOG=`mysql -u root -pXXX --execute="SHOW MASTER STATUS" -AN | awk '{print $1}'`
          CURRENT_POS=`mysql -u root -pXXX --execute="SHOW MASTER STATUS" -AN | awk '{print $2}'`
          # ...
  ENDSSH
  }
  </code></pre>
  
  <p>The two lines assigning CURRENT_* variables are the problem: the <code>mysql -u...</code> command gets executed locally, instead of remote session. </p>
  
  <p>Please advice how to run that remotely, assigning the local variable with a result of a remote mysql command.</p>