Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to create an empty database with one line of command in bash?

My newbie attempts have failed:

mysql -uroot $(create database mydatabase;)

I end up having to

  • mysql -uroot
  • create database mydatabase;
  • exit;
share|improve this question
up vote 24 down vote accepted

@Nitrodist's answer will work, but it's over-engineering the problem. MySQL's command-line client supports the very handy -e switch, like so:

mysql -uroot -e "create database 'foo'"

You can of course substitute any valid SQL into there.

share|improve this answer
sorry I edited my answer after some more research. – Nitrodist May 25 '11 at 20:50
Thanks! Exactly what i'm looking for. – chrisjlee May 25 '11 at 21:35
The quotes are not required mysql -uroot -e "create database foo" – IanVaughan Mar 7 '12 at 14:55

According to MySQL's documentation, the mysql can take commands from stdin

shell> mysql -uroot < script.sql >

You can use Process Substitution to accomplish this:

shell> mysql -uroot <(echo "create database mydatabase;") 

or you can just pipe it to the stdin of mysql normally:

shell> echo "create database mydatabase;" | mysql -uroot

Can't test this personally, but I think this should work.

Edit: Another option is to use the -e option, specified here, like so:

shell> mysql -uroot -e "create database mydatabase;"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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