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 9208299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:35:09+00:00 2026-06-18T00:35:09+00:00

I have a question about Shell Variable substitution. Requirement: I need to insert something

  • 0

I have a question about Shell Variable substitution.

Requirement:
I need to insert something to mysql db.And I don’t want to write the insert script in main shell ,so I would like to define a insert sql script as a variable $SQL .
This variable and many other variables are stored in another file named sql.cfg

but this variable will refer another variable $Value.
when i execute the main shell, the $Value will be replace by actually value。

Example:
in sql.cfg

SQL="insert into table column1,column2 values($Value1,$Value2)"
DB_NAME=dbname
DB_IP=192.168.0.x
USR=username
PWD=password

in main.sh

. ./sql.cfg

Value1=100
Value2=200

` mysql $DB_NAME -h$DB_IP -u $USR -p$PWD -se "$SQL;"`

Question:

How can I substitute the $sql with $Value1 $Value2 properly so that I can insert them into to mysql db?

Thanks a lot!!!

  • 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-06-18T00:35:10+00:00Added an answer on June 18, 2026 at 12:35 am

    you have to set Value1,Value2 in your main.sh before you load/source your sql.cfg

    example:

    kent$  head *           
    ==> main.sh <==
    #!/bin/bash
    Value1=100
    Value2=200
    source sql.cfg
    echo "$DB_NAME - $SQL"
    
    
    
    ==> sql.cfg <==
    #!/bin/bash
    SQL="insert into table column1,column2 values($Value1,$Value2)"
    DB_NAME=dbname
    DB_IP=192.168.0.x
    USR=username
    PWD=password
    
    kent$  ./main.sh
    dbname - insert into table column1,column2 values(100,200)
    

    And if you really want to load the sql.cfg first and set the value in your main.sh later. you could change the $Value1,2 in your sql.cfg as placeholders, and in main.sh substitute those values as you want. See the example:

    kent$  head *
    ==> main.sh <==
    #!/bin/bash
    source sql.cfg
    
    Value1=444
    Value2=555
    
    SQL=$(sed "s/:Value1/$Value1/g; s/:Value2/$Value2/g" <<<$SQL)
    
    echo "$DB_NAME - $SQL"
    
    
    ==> sql.cfg <==
    #!/bin/bash
    SQL="insert into table column1,column2 values(:Value1,:Value2)"
    DB_NAME=dbname
    
    kent$  ./main.sh
    dbname - insert into table column1,column2 values(444,555)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Shell script. Just have a small question about some script
I have a question about erlang shell and erlang vm. We have to write
I have question about XSLT1.0. The task is to write out in HTML all
I have a question about parallel processing in shell scripting. I have a program
I have a question about MySql. I have a table with 7.479.194 records. Some
I have question about parsing in Html helper : I have sth like: @foreach
I have question about clean thory in Python. When: @decorator_func def func(bla, alba): pass
I have question about normalization. Suppose I have an applications dealing with songs. First
I have question about interpreting strings as packed binary data in C++. In python,
i have question about YAJLiOS parser... I have next json data : {{ body

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.