I have a script to clone a vhost and associated db - however one of the mysql commands doesn't work. It fails and ends up outputting the mysql version number and 'how to use' info as if it hasn't been passed valid arguments - no error is given though.

An abbreviated version of the script is below - it's the line that should create the new database and grant privilidges that is failing. I'd appreciate any guidance on what I've got wrong or how to track down the problem.


#!/bin/bash -e
echo "MySQL Admin Password: "
read -s mysqlpass
echo "New Site name (short, a-zA-Z only)"
read -e newsitename
echo "Last chance - sure you want to run the script? (y/n)"
read -e run
if [ "$run" == y ] ; then

    # This command doesn't work
    dbsetup="CREATE DATABASE "$newsitename"_db;GRANT ALL PRIVILEGES ON "$newsitename"_db.* TO 'site_usr'@'localhost';FLUSH PRIVILEGES;"
    mysql -u root -p$mysqlpass -e "$dbsetup"

    mysqldump -u root -p$mysqlpass template_db > temp.sql

    dbimport=$newsitename"_db < temp.sql"
    mysql -u root -p$mysqlpass $dbimport

  • See bash pitfall number 20. Also quote your variables.
    – Wildcard
    Apr 25 '16 at 22:39
  • Also, why are you even setting a variable dbsetup if you only use it once?
    – Wildcard
    Apr 25 '16 at 22:41
  • Quotes were the problem - thanks. It was just in a variable as this started by trimming down a much larger script.
    – phil
    May 11 '16 at 11:29

You can try:

dbsetup="CREATE DATABASE ${newsitename}_db;GRANT ALL PRIVILEGES ON ${newsitename}_db.* TO 'site_usr'@'localhost';FLUSH PRIVILEGES;"

Double quotes will corrupt your string.


This one worked for me in shell script:


mysql -u root -proot dbname -e "update environment SET deployment_status = 1 WHERE environment_id = '${environment_id}'";

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.