You might want to
- immediately execute the command and
- give it the query it is supposed to execute.
This way:
example=$(ssh [email protected] mysql -h localhost -u root -p"$MYSQL_PASSWORD" myDatabase -e '"SELECT now();"')
echo "My goal is to read the variable here: $example"
would be the way to go.
If you really need it at both places, you either might want to have a script on the server, or you could try
example=$(ssh [email protected] 'example=$(mysql -h localhost -u root -p"$MYSQL_PASSWORD" myDatabase -e "SELECT now();"); echo "Variable is $example")
echo "My goal is to read the variable here: $example"
In order to stick on the heredoc, you could as well try
example=$(ssh [email protected] << EOFMARK
example=$(mysql -h localhost -u root -p$MYSQL_PASSWORD myDatabase -e "SELECT now();")
echo "Variable is $example"
EOFMARK)
echo "My goal is to read the variable here: $example"
in order to have the variable on the client side as well.
Due to whatever, I can't get my head around the heredoc example.
But the others should look like
example=$(ssh [email protected] 'mysql -NB -h localhost -u root -p"$MYSQL_PASSWORD" myDatabase -e "SELECT now();"')
echo "My goal is to read the variable here: $example"
which gives exactly the output into the local variable, or
example=$(ssh [email protected] 'example=$(mysql -NB -h localhost -u root -p"$MYSQL_PASSWORD" myDatabase -e "SELECT now();"); echo "Variable is $example")
echo "My goal is to read the variable here: $example"
but you should be aware that here, the local output looks like (e. g.)
My goal is to read the variable here: Variable is 2014-03-07 20:42:23
as the sub string Variable is gets output as well.