0
 #! /bin/bash

set -x
sql="show tables;"

~/mysql/bin/mysql --default-character-set=utf8 -hxxxx -Pxxx -pxxx -uxxx -Drxxx $sql
~/mysql/bin/mysql --default-character-set=utf8 -hxxxx -Pxxx -pxxx -uxxx -Drxxx "$sql"

Results of enforcement:

screenshot

The execution with double quotation marks succeeded. Execution without double quotation marks failed.

What is the difference between $sql and "$sql"?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
answer
  • 1
  • In brief, always quote unless you specifically require the shell to do token splitting and wildcard expansion on the value. – tripleee Nov 05 '20 at 05:29

1 Answers1

0

The difference is that:

./script foo bar

Gives a script 2 arguments, and

./script "foo bar"

Gives it 1 argument with a space in the middle.

If one works and the other doesn't for your MySQL command, it tells me that MySQL wants the SQL query to be specified as a single argument.

Evert
  • 93,428
  • 18
  • 118
  • 189