0

I have the following in a bash script:

target="-h1.2.3.4 -P3307"
user="-uusername"
pass="-ppassword"

mysql ${target} ${user} ${pass} -se"SHOW DATABSES;"

This works fine, except that it connects to Port 3306 and not 3307. How can I make it read ${target} correctly?

Without splitting target into two separate variables.


If I manually run

mysql -h1.2.3.4 -P3307 -uusername -ppassword

then it works fine, so that is what I am trying to achieve. It is almost as if MySQL is ignoring the -P3307 and using the default 3306.

I also tried putting it all in one variable, and that doesn't work either (it ignores the Port, Username and Password).


It looks like the problem is that MySQL is interpreting the $target value as if it is one thing. If I switch around Port and Host I get:

target="-P3307 -h1.2.3.4"
mysql ${target} ${user} ${pass} -se"SHOW DATABSES;"

I get:

Unknown suffix ' ' used for variable 'port' (value '3307 -h1.2.3.4')
mysql: Error while setting value '3307 -h1.2.3.4' to 'port'

So I don't know if that is a MySQL problem or a Linux problem?

  • Search terms: "shell quoting variables", put them inside double quotes, also shellcheck.net might be useful. – phk Nov 1 '16 at 17:04
  • bash is passing "-P3307" as the second parameter to mysql; quoting it as phk suggests would just turn mysql's arg1 into "-h1.2.3.4 -P3307". I'm not familiar enough with mysql to know: (1) is there other overriding configuration for host 1.2.3.4 that determines the port, or (2) does mysql want a space after -P and before 3307? – Jeff Schaller Nov 1 '16 at 17:40
  • if you echo, what is the output ? echo mysql ${target} ${user} ${pass} -se"SHOW DATABSES;" – Kamaraj Nov 2 '16 at 8:22
  • If I echo it, it looks fine mysql -h1.2.3.4 -P3307 -uusername -ppassword -se"SHOW DATABASES" – IGGt Nov 2 '16 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.