Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store mysql query output into a shell variable

I need a variable to hold results retrieved from the database. So far this is basically what I'm trying with no success.

myvariable=$(mysql database -u $user -p $password | SELECT A, B, C FROM table_a) 

My understanding of bash commands is not very good as you can see.

like image 274
imnotneo Avatar asked Oct 28 '09 12:10

imnotneo


People also ask

How do I run a query in MySQL shell?

Running select queries After you have logged into a database with the MySQL command line tool (covered in my using the MySQL command line tool post), you can run queries by simply typing them in at the command prompt. The query will not be executed until you either enter ; g or G and then press the <enter> key.


1 Answers

A more direct way would be:

myvar=$(mysql mydatabase -u $user -p$password -se "SELECT a, b, c FROM table_a") 
like image 52
ennuikiller Avatar answered Sep 23 '22 14:09

ennuikiller