0

I have observed a variety of error messages returned from mysql when called from a BASH script.

Following in one such example:

#!/bin/bash
declare -g SQL_cmd="SHOW DATABASES"
declare -g MySQL_cmd="mysql -u temp -pAbcde_0123 --skip-column-names --execute='${SQL_cmd}'"
declare -g results=$($MySQL_cmd)
printf "command: $MySQL_cmd\n"
printf "results: $results\n"

When I run this script, the output is as follows:

ERROR 1044 (42000): Access denied for user 'temp'@'localhost' to database 'DATABASES''
command: mysql -u temp -pAbcde_0123 --skip-column-names --execute='SHOW DATABASES'
results:

However, if I copy and paste the command from the output above, it returns the following:

17:26 >mysql -u temp -pAbcde_0123 --skip-column-names --execute='SHOW DATABASES'
+--------------------+
| information_schema |
| temporary          |
+--------------------+

Which is exactly what I would expect.

Can someone explain what is going on here?

Thank you.

base2boss
  • 21
  • 3
  • Storing commands in variables doesn't work; see [BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail!](http://mywiki.wooledge.org/BashFAQ/050) If you really need to store it first, use a function (that's what they're for) or an array (see [this question](https://stackoverflow.com/questions/12136948/why-does-shell-ignore-quoting-characters-in-arguments-passed-to-it-through-varia)). But it's generally best to just execute commands directly. Oh, and avoid the temptation to use `eval` -- it's a massive bug magnet. – Gordon Davisson Jun 15 '21 at 21:52

0 Answers0