Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1042265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:28:52+00:00 2026-05-16T15:28:52+00:00

I have a query that I need to loop. query=select ‘$dbserver’ as server; while

  • 0

I have a query that I need to loop.

query="select '$dbserver' as server;"

while read dbserver username password dbname type
do
mysql -h$dbserver -u$username -p$password $dbname -Be"$query" >> /home/develop/myreport.csv
done < $dblist

The following line expands correctly.

mysql -h$dbserver -u$username -p$password $dbname -Be"select '$dbserver' as server;" >> /home/develop/myreport.csv

But when I take out the query and save it in a variable as shown above, it stops working as expected and gives blank value for “dbserver”.
The line mentioned in the loop above does not work.
How to correct this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:28:53+00:00Added an answer on May 16, 2026 at 3:28 pm

    If you want the '$dbserver' in the query expanded inside the loop, you’d probably write:

    while read dbserver username password dbname type
    do
        query="select '$dbserver' as server;"
        mysql -h$dbserver -u$username -p$password $dbname -Be"$query"
    done < $dblist  > /home/develop/myreport.csv
    

    As originally written, the query string is evaluated before any value is assigned to $dbserver, which is why you got the empty string in the output.

    Note that the output redirection was done just once – on the done line rather than each time in the loop (which means that you don’t need append any more).


    Getting the query created outside the loop is normally something you can do, using eval. However, because the value of the $dbserver is enclosed inside single quotes, this turns out to be hard. If the DBMS you are using permits the use of double quotes around strings (contrary to the SQL standard), then this works with an eval:

    query='select \"$dbserver\" as server;'
    echo "$query"
    while read dbserver username password dbname type
    do
        echo 1: "$query"
        eval echo 2: "$query"
        qval=$(eval echo "$query")
        echo mysql -h$dbserver -u$username -p$password $dbname -Be"$qval"
    done
    

    You can then adapt this to use single quotes by replacing the ‘"‘ with the ‘'\''‘ sequence:

    query='select \'\''$dbserver\'\'' as server;'
    echo "$query"
    while read dbserver username password dbname type
    do
        echo 1: "$query"
        eval echo 2: "$query"
        qval=$(eval echo "$query")
        echo mysql -h$dbserver -u$username -p$password $dbname -Be"$qval"
    done
    

    That is, however, the sort of quote sequence that sends sane people from the room screaming – excuse me a moment while I vacate the premises noisily! […later…] That’s better!

    Explanation:

    • The overall string is inside single quotes.
    • There are no escape characters inside such a string.
    • Therefore, the first backslash is just a backslash.
    • The next 4 characters are the sequence '\''.
    • The first of these quotes terminates the current single-quoted string.
    • The backslash suspends the special meaning of the next character, so that the string contains an actual single quote from the second single quote in the sequence.
    • The third single quote starts a new single-quoted string.
    • So, after processing the first sequence of backslashes and quotes, the string contains a backslash and a single quote.
    • $dbserver is just normal text at this point.
    • We then have a repeat of the previous sequence, ending up with a second backslash-quote pair in the string.
    • Everything is normal to the last single quote on the line.

    The eval process runs an extra lot of expansion on the string. The backslash-quote pairs are replaced by just quote; the current value of $dbserver is inserted. This can then be passed to the command as an ordinary argument.

    The difficulty with eval is ensuring that you don’t get unexpected side-effects. This is doubly complex with MySQL which uses back-quotes to enclose keywords used as tokens. That notation interacts diabolically with eval, of course. However, with single quotes around the whole query and backslash-backquote in place of each backquote, you can do it:

    query='select \'\''$dbserver\'\'' as server, \`ls\` as column;'
    echo "$query"
    while read dbserver username password dbname type
    do
        echo 1: "$query"
        eval echo 2: "$query"
        qval=$(eval echo "$query")
        echo mysql -h$dbserver -u$username -p$password $dbname -Be"$qval"
    done
    

    I don’t think it can be recommended, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Microsoft SQL server 2008 I have a query that is in need of
I have a query that I need to execute that I do not know
I have a query such that I need to get A specific dog All
I have a dynamic query string that I need to pass via an .htaccess
I have a query that does what i want joining table but i need
Say I have a query that fetches [type][show_name]. For all [type]==5 records, I need
I currently have a SQL query that returns a number of fields. I need
I need to be able to have an SQL query that searches my database
Need help with a query that I wrote: I have three tables Company id
I have two tables described below. What I need is a single query that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.