Kristjan Adojaan
Kristjan Adojaan

Reputation: 537

mysqldump --where does not work inside shell script

I prepare mysqldump command in a shell script like follows (simplified example)

command="mysqldump -v -uuser -ppass base1 table1 --where=\"id=1 or id=2\""

When I echo the command

echo $command

in shell script it returns correct mysqldump command

mysqldump -v -uuser -ppass base1 table1 --where="id=1 or id=2"

that I can copy and run and it works well.

When I try to run it from inside of the shell script

$command

it seemingly does not pass quotation marks to the command and therefore cannot execute conditional dump, saying

mysqldump: Couldn't find table: "or"

It works in shell script if the --where attribute does not contain spaces but not otherwise. I have tried using single quotes, backticks, escapes.

Upvotes: 1

Views: 428

Answers (1)

axiac
axiac

Reputation: 72186

Your command variable contains the correct command to run and echo $command shows this. But because the argument of --where contains whitespaces, when $command is evaluated these whitespaces make the shell parse the command line in a different way than you think.

The solution is simple. If you have composed the command line as string in the command variable, use the eval internal shell command to execute it:

eval $command

Upvotes: 2

Related Questions