Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

what I want to acomplish is:

1.) Having a config file as template, with variables like $version $path (for example apache config)

2.) Having a shell script that "fills in" the variables of the template and writes the generated file to disk.

Is this possible with a shell script. I would be very thankfull if you can name some commands/tools I can accomplish this or some good links.

share|improve this question
up vote 15 down vote accepted

This is very possible. A very simple way to implement this would be for the template file to actually be the script and use shell variables such as

#! /bin/bash
cat > /tmp/destfile <<-EOF
here is some config for version $version which should
also reference this path $path

You could even make this configurable on the command line by specifying version=$1 and path=$2, so you can run it like bash script /foo/bar/baz 1.2.3. The - before EOF causes whitespace before the lines be ignored, use plain <<EOF if you do not want that behavior.

Another way to do this would be to use the search and replace functionality of sed

#! /bin/bash
sed -e "s/VERSION/$version/g" -e "s/PATH/$path/" /path/to/templatefile > /tmp/destfile

which would replace each instance of the strings VERSION and PATH. If there are other reasons those strings would be in the template file you might make your search and replace be VERSION or %VERSION% or something less likely to be triggered accidentally.

share|improve this answer

No tools necessary other than /bin/sh. Given a template file of the form

Version: ${version}
Path: ${path}

or even with mixed shell code included

Version: ${version}
Path: ${path}
Cost: ${cost}\$
$(i=1; for w in one two three four; do echo Param${i}: ${w}; i=$(expr $i + 1); done)

and a shell parsable configuration file like


it is a simple matter to expand this to

Version: 1.2.3-r42
Path: /some/place/under/the/rainbow/where/files/dance/in/happiness
Cost: 42$
Param1: one
Param2: two
Param3: three
Param4: four

Indeed, given the path to the configuration file in shell variable config_file and the path to the template file in template_file, all you need to do is:

. ${config_file}
template="$(cat ${template_file})"
eval "echo \"${template}\""

This is perhaps prettier than having complete shell script as the template file (@mtinberg's solution).

The complete naive template expander program:


PROG=$(basename $0)

    echo "${PROG} <template-file> [ <config-file> ]"

    local template="$(cat $1)"
    eval "echo \"${template}\""

case $# in
    1) expand "$1";;
    2) . "$2"; expand "$1";;
    *) usage; exit 0;;

This will output the expansion to standard output; just redirect standard output to a file or modify the above in obvious fashion to produce the desired output file.

Caveats: Template file expansion would not work if the file contained unescaped double quotes ("). For security reasons, we should probably include some obvious sanity checks or, even better, perform shell escaping transformation if the template file is generated by external entity.

share|improve this answer
It is great to still see folks actively using shell scripts to do fun stuff like this. A buddy of mine wrote a clock in shell script. Fun times indeed. – chicks Jun 16 '15 at 13:21

You probably ought to look into a configuration management system like Puppet or Chef. These can easily do what you describe above and much more.

share|improve this answer
thanks. Absolutely, I have Chef installed and running. But it adds a lot of overhead, when you have to write your own cookbooks. I do not know the ruby programming language and my conclusion was. its easier to do this with a shell script for the easier cases (if possible). – Markus Jul 6 '11 at 21:42
Looks like Puppet and Chef both use ERB for templates, and that's ridiculously easy to get started with. Given a variable name, the string <%= name %> in a template will get replaced with name's value. How you define name outside the template differs between the two systems, obviously. – Mike Renfro Jul 6 '11 at 23:42
Yes templating (With Chef) itself is absolutely easy. But using chef as Framework (and writing the cookbooks) requires a lot of time. To get the data into the templates you need to understand where and how Chef manages the "merging" of datasources and a lot of other stuff. I have started writing my own cookbooks, but a shell script would in my special case be 100 times faster... – Markus Jul 15 '11 at 18:49
Getting the infrastructure going for Chef or Puppet can be a pain or you can try to figure out how to run them headless which is a fun adventure. Ansible works fine in pull or push mode out of the box so it might strike a better balance of figuring it out and scripting it yourself. – chicks Jun 16 '15 at 13:27

I use shtpl for that. (private project of mine, which means, it is not widely in use. But maybe you want to test it anyway)

For example you want to generate an /etc/network/interfaces out of a csv-file, you can do it like that:

CSV-file content (here test.csv):


Template (here interfaces.tpl):

#% IFS=';'
#% while read "Val1" "Val2" "Val3" "Val4"; do
auto $Val1 
iface $Val1 inet static
  address $Val2 
  netmask $Val3 
  gateway $Val4 

#% done < "$CSVFILE"


$ CSVFILE=test.csv sh -c "$( shtpl interfaces.tpl )"


auto eth0 
iface eth0 inet static

auto eth1 
iface eth1 inet static


share|improve this answer
Thanks for putting the code on github. That's awesome. – chicks Jun 16 '15 at 13:23

If you want lightweight and real templates rather than shell code that generates new files, the usual choices are sed& awk or perl. Here is one link:

Me, I'd use a real language like perl, tcl, python, ruby or something else in that class. Something built for scripting. They all have good, simple templating tools and tons of examples in google.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.