mysql -u root -ppassword -D database -s -N -e "SELECT id FROM myTable"

with special password and database is working fine.

I want to split the code into two parts:

  1. Executional part:

    mysqlE=mysql -u root -ppassword -D database -s -N -e


  1. MySQL command part:

    query="SELECT id FROM myTable"

to execute it with something similar like:

mysqlE query

How can I do this?

  • 1
    passing a password on the command line is a bad idea as it can easily be viewed from /proc or by running ps. See unix.stackexchange.com/a/205184/7696 for a better alternative. Remember to chmod 600 the config file. – cas Oct 21 '15 at 0:02

You almost have it already for a shell script:


mysqlE="mysql -u root -ppassword -D database -s -N -e"
query="SELECT id FROM myTable"
$mysqlE "$query"

Another way is to put the mysql command in a function (put it in ~/.bashrc for instance)

function mysqlE()
    mysql -u root -ppassword -D database -s -N -e "$@"

from a new shell or source ~/.bashrc use

mysqlE "$query"

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.