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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:27:31+00:00 2026-05-29T09:27:31+00:00

I read a string from a file with shell script variables, and want to

  • 0

I read a string from a file with shell script variables, and want to substitute the variables with values in a function like

hello.txt:
-------------
Hello $NAME


a.sh 
-------------
function printout
{
  echo ???somehow_parse??? $1
}

NAME=Joe
printout "$(cat hello.txt)"
NAME=Nelly
printout "$(cat hello.txt)"

The example is not the best, but it describes my problem. In other words: can I use shell as a template engine?

I am using ksh.

  • 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-29T09:27:32+00:00Added an answer on May 29, 2026 at 9:27 am

    In general, I would go for a search-and-replace approach using sed/awk such as that shown in Kent’s answer or this answer.

    If you want a shell-only approach, then the standard way would be to use eval. However, this poses a security risk. For example:

    [me@home]$ cat hello.txt
    hello $NAME; uname -a
    [me@home]$ NAME="shawn"
    [me@home]$ eval echo "`cat hello.txt`"   # DO NOT DO THIS!
    hello shawn
    Linux SOMEHOST 2.6.9-101.ELsmp #1 SMP Fri May 27 18:57:30 EDT 2011 i686 i686 i386 GNU/Linux
    

    Notice how a command can be injected into the template!

    You can however reduce the risk using this approach:

    [me@home]$ eval "OUT=\"`cat hello.txt`\""
    [me@home]$ echo $OUT
    hello shawn; uname -a
    

    Do note that this is still not foolproof as commands can still be injected using $(cmd) or `cmd`.

    In short, you should use eval only if you understand the risks and can control/limit access to the template files.

    Here’s an example of how this can be applied in your script:

    function printout {
        FILENAME=$1
        eval "OUT=\"`cat $FILENAME`\""
        echo $OUT
    }
    
    NAME=Joe
    printout hello.txt
    NAME=Nelly
    printout hello.txt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We can read a string array from xml file in android like mentioned here
I'm want to read a simple string from a text file which is around
I am trying to convert a string I read in from a file to
The string in question (read from a file): if (true) then { _this =
I am trying to read strings from a file that has each string on
I read some properties from an xml file, amongst which is a string that
I am trying to read certain values from a json string in php, I
I would like to read an file into a string. I am looking for
How can I read a string from a .resx file in C#?
I want to delete a char/string from a binary/text file. If I know the

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.