Backticks and end-of-line characters in vvv-init.sh

If you’ve ever chased your tail for half an hour on vague errors in your vvv-init.sh files in the popular Varying Vagrant Vagrants development tool for WordPress, here’s a few things to check.

Backticks in SQL statements

If the MySQL statements you’re executing have object names with special characters, you’ll most likely need to use backticks (`) to enclose them within your script.  Be sure to escape those backtick characters with backslashes (\\) to ensure errors aren’t generated while VVV is executing your provisioning script.

Here’s an example of escaped backticks in vvv-init.sh that works:

# Make a database, if we don't already have one
echo "Creating database (if it doesn't already exist) ..."
mysql -u root --password=root -e "CREATE DATABASE IF NOT EXISTS \\`my-database.com-dev\\`"
mysql -u root --password=root -e "GRANT ALL PRIVILEGES ON \\`my-database.com-dev\\`.* TO wp@localhost IDENTIFIED BY 'wp';"
echo "Database is setup!"

# Go on about setting up your sites ...

If you don’t escape backticks, you could likely see errors like this:

==> default: Creating database (if it doesn't already exist)
==> default: ERROR
==> default:  1064 (42000)
==> default:  at line 1
==> default: : 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 '.com-dev'

Unix/Linux/OSX line ending characters

If you’re editing your vvv-init.sh file in Windows, make sure you’re saving those files with the correct Unix-style line endings, and not Windows-style (or the older Mac-style). If you don’t escape backticks, you could likely see errors like this:

==> default: vvv-init.sh: line 4: 
\r': command not found