0

I am trying to execute select query and store the result in a variable in shell script. Below is the script

   mysql="mysql -h localhost -u root"
   db="testdb";
   echo "Select Name from Details"|${mysql} ${db};

The above statement gets executed with the column 'Name' getting printed with its value. But when i try to assign the result like below

   var = `"Select Name from Details"|${mysql} ${db};`
   echo $var

   or

   var = $("Select Name from Details"|${mysql} ${db};)
   echo $var

I get the below error 'var: not found'. Where am i going wrong in trying to assign the result of the query to the variable?

1 Answer 1

3
   var=`echo "Select Name from Details"|${mysql} ${db};`
   echo $var

There should not be any space between the variable name and =. The shell else interprets the variable name to be a command/executable. And hence the error!

Sign up to request clarification or add additional context in comments.

7 Comments

i tried removing the space and executing but it throws an error of "Select Name from Details Not Found". Why does it occur?
try my updated answer please. Added echo to the String
@Maheswaren: I get the result as 'Name aarish'. How do i parse it so as to retrieve the name alone?
try the -ss option to turn off the headers.
Sorry @aarish , Not Sure abt turning headers off in mysql. In oracle it is simple.!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.