Search code examples
mysqlbashcommand-linecommand

Running mysql commands inside bash script


I am trying to execute a mysql command inside a bash script but every time I try to execute it fails for some reason. I have tried several ways and none seemed to work(e.g: <<QUERY...;QUERY) My select is the following but I get an error:

#!/bin/bash
mysql -utesting -pMypass -hlocalhost -D test DB -e "

SELECT value FROM h6_options

where module=cloud

AND `option`=prefix;"

I get the following error.
ERROR 1064 (42000) at line 3: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '='prefix'' at line 5

Any help is appreciated.

Solution

  • Turns out the issue was the backticks. I had to escape them in order to not evaluate the line.

    <<QUERY
    SELECT value FROM h6_options
    
    WHERE \`option\`="prefix"
    
    AND module="cloud"
    QUERY