DEV Community


Posted on

ShowDEV: Bash-TPL - A smart, lightweight shell script templating engine

I know the landscape is crowded, but I'd like to offer my submission for an Easy To Use / Easy To Maintain template engine:

Introducing Bash-TPL : A Smart, lightweight shell script templating engine, written in Bash


  • Single bash script
  • Easy to ship with your project or build process
Smart Indentation Tracking
  • Removes unwanted whitespace from rendered text files
  • Encourages well-formatted mix of template tags and textual content
Generates Reusable Shell Scripts
  • Can manage & invoke shell scripts without bash-tpl
  • Only need bash-tpl when your source templates change
Shell Agnostic
  • Uses standard printf, variable, and sub-shell constructs
  • Templates can target any modern shell
Supports Includes
  • Can organize your templates as smaller, reusable components
  • Indentation tracking even works across includes !
Flexible Delimiter Support
  • Can customize all template delimiters
  • Can modify delimiters mid-template
  • Can include templates with differing delimiters

Quick Example

As is the way: A quintessential hello world example is of course in order:


Hello, <% ${NAME:-World} %>
Enter fullscreen mode Exit fullscreen mode

compile + run

$ source <( bash-tpl hello.tpl )

Hello, World
Enter fullscreen mode Exit fullscreen mode

compile + run with NAME

$ NAME=TekWizely source <( bash-tpl hello.tpl )

Hello, TekWizely
Enter fullscreen mode Exit fullscreen mode

view compiled template

$ bash-tpl hello.tpl

printf "%s\n" Hello\,\ "${NAME:-World}"
Enter fullscreen mode Exit fullscreen mode

More Information

This hello example is really just the start of what bash-tpl can do.

There's a full README on the Project Home Page.

If you're looking for an easy solution for creating maintainable templates to generate well-formatted text files, I hope you'll give my project a try.

I'm happy to answer any questions or work through any use-cases you might have for templates and see how bash-tpl might help.

Feel free to comment and thanks for looking!


Top comments (3)

moopet profile image
Ben Sinclair

What problem does this solve? Can you give a real-world example of where it would be used?

It seems to me that if you know how to type this:

Hello <% "${NAME:-World}" %>
Enter fullscreen mode Exit fullscreen mode

then you know enough POSIX to be able to write this:

printf "Hello, %s" "${NAME:-World}"
Enter fullscreen mode Exit fullscreen mode

Anything large enough to need templates for output would probably be better written in a more comprehensive scripting language.

tekwizely profile image
TekWizely • Edited


a more comprehensive scripting language

Bash-TPL allows you to use the FULL power of the shell scripting language of your choice, so I think we're saying the same thing.

What problem does this solve?

As with any templating language, the problem being solved is enabling you to mix some kind of logic in with a well-formatted textual file (say an HTML file, XML file, apache config, ad-hoc blog template, etc)

Say you have ~100 line config template (maybe an apache vhost template or somesuch), do you really want to be writing 100+ printf lines ?

bash-tpl manages converting your well-formatted and easy to edit text file into printf lines, while allowing you easily inject scripting logic around the file ...

The smart indentation tracking allows you clearly integrate your shell scripting without affecting the final resulting whitespace.

Trivial example:

Say you want:

Enter fullscreen mode Exit fullscreen mode

Given a template:

    % for i in "${items}"; do
        %# The indentation introduced by for loop content
        %# will NOT be present in the output
        <li><% $i %></li>
    % done
Enter fullscreen mode Exit fullscreen mode

This will generate the expected output, even though you added whitespace in order to make the for-loop content clearly visible and easier to edit.

I hope that helps a bit - thanks for looking and I'm happy to answer any other questions you might have.

edit: grammar,typos

moopet profile image
Ben Sinclair

Gotcha, the example of generating a configuration file makes perfect sense.