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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:15:53+00:00 2026-06-14T09:15:53+00:00

I have a bash script that is responsible for querying a database and piping

  • 0

I have a bash script that is responsible for querying a database and piping the results to a file:

date=`date +"%Y/%m/%d"`

/usr/bin/mysql -u $db_user -p$db_pass -h $db_name $db_schema << MYSQLEOF > $output_file

  select *
  from table
  where date = $date

MYSQLEOF

Now expanding this project into other areas, for the sake of re-usability/orthogonality I wanted to house the query in it’s own file and call with something like:

cmd=`echo $sql_file`

/usr/bin/mysql -u $db_user -p$db_pass -h $db_name $db_schema -e "$cmd" > $output_file

sql file:

  select *
  from table
  where date = $date

I am having trouble finding out how I can (in bash) do this, while still being able to have and adjust variables ($date) inside the sql file. Echoing the .sql file into a bash variable, I haven’t been able to get “$date” to not be taken as a literal string.

Is there a solution in bash, or should I look into something like perl to handle this?

  • 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-14T09:15:54+00:00Added an answer on June 14, 2026 at 9:15 am

    I found a solution using command-line Perl to search/replace on the fly.

    Bash script:

    date=`date +"%Y/%m/%d"`
    export date
    
    cmd=`perl -lpe 's/DATE_VAR/$ENV{date}/g' "$sql_file"`
    
    /usr/bin/mysql -u $db_user -p$db_pass -h $db_name $db_schema -e "$cmd" > $output_file
    

    and in sql_file:

    select *
    from table
    where date = 'DATE_VAR'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bash script that sources contents from another file. The contents of
I have a bash script that looks like this: #!/bin/sh previousRelease=`git describe --tags --match
I have a sh/bash script that needs to call a batch file with parameters
I have a bash-script (let's call it /usr/bin/bosh ) using the following she-bang line:
I have a bash script that looks like the following: #!/bin/bash FILES=public_html/*.php # */
I have a bash script that needs to perform a couple actions in MySQL.
I have a bash script that uploads a file via SFTP, with a command
I have a bash script that takes the date, month and year as separate
I have bash code that looks like the following: /usr/bin/expect -c ' spawn python
I have a bash script that creates a Subversion patch file for the current

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.