Im making a script where I connect to a database, look for a value and then if that value returned something then I will do something else.

ip=$("variable is defined here")

Then I connect to the MYSQL database using:

/Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot --password=*

However, this takes me to another prompt, and I need to run another command from that, I can't get it to work.

I tried this:

/Applications/MAMP/Library/bin/mysql "MYSQL argumens;";

and just:

MYSQL arguments;

None of these work and I have to pass a variable to it, what can I do now?

  • You might find the DBA SE useful.
    – mdpc
    Nov 23, 2014 at 20:05

2 Answers 2


You can do something like this:

#! bin/bash
selectvar="SELECT * FROM test;"

mysql --user=root --password=mypass database << eof 

To execute statements from the command line without an interactive prompt, use the -e option:

mysql mydb -e 'select * from foo'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .