0

I'm trying to run a query from a bash script:

#!/bin/bash

query="\"show databases\""
command="mysql --defaults-file=/user/.my.cnf -e "
outputfile=" > query_result.txt"
command=$command$query$outputfile
$($command)

the result is this:

# ./query_test
mysql: unknown option '--print-defaults'

what I'm doing wrong?

The command:

 mysql --defaults-file=/user/.my.cnf -e "show databases"

works without any issue from the shell

Kintaro
  • 178
  • 3
  • 14

1 Answers1

1

thanks to the comment of @benjamin-w I solved with this:

#!/bin/bash
args=(--defaults-file=/users/.my.cnf)
args+=(-e "show databases")
outputfile="query_result.txt"
mysql "${args[@]}" > "$outputfile"

other examples in this link: https://mywiki.wooledge.org/BashFAQ/050

chepner
  • 497,756
  • 71
  • 530
  • 681
Kintaro
  • 178
  • 3
  • 14