How to pass shell variable into "mysql -e" querry

Hi,

I need to pass the shell variable into mysql -e query in bash shell command line

$ export ID=0
$ mysql -e 'select * from user where id=$ID'
ERROR 1054 (42S22) at line 1: Unknown column '$ID' in 'where clause'
LVL 38
wesly_chenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this is, AFAIK, not possible.
you will have to build a text file with the query, and use that file as input for running the query.
0
 
wesly_chenAuthor Commented:
> build a text file with the query, and use that file as input for running the query.
How?
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
that would be like this:
echo "select * from user where id=$ID;" > file.sql
echo "exit;" >> file.sql
mysql < file.sql
rm file.sql

Open in new window

0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
wesly_chenAuthor Commented:
> echo "exit;" >> file.sql
it complains about syntax error. I take out ";" and everything is ok now.

Thanks a lot.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.