Skip to content

nealian/bash-unsafe-templates

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 

Repository files navigation

bash-unsafe-templates (but.sh)

A rather unsafe, very trusting templating engine that lets you use bash as your parser.

Based on http://serverfault.com/questions/287688/templating-with-linux-in-a-shell-script/#699377

Prerequisites

but.sh requires only bash

Downloading

wget https://raw.github.com/nealian/bash-unsafe-templates/master/but.sh && chmod +x but.sh

Environment

The current shell environment is used, and additional variables may be set in two ways:

  • As with any other script, you may specify variable values on the command line (FOO=bar ./but.sh)
  • Additionally, environment files may be sourced with the -c and --config flags (and any filenames at the end of the arguments)

Templates

Templates are text files with bash expansions, and with escaped double-quotes and backslashes. but.sh will expand the following:

  • $VAR and ${VAR} (and ${ARRAY[@]} and other more complicated variable expansions)
  • $(()) arithmetic expansions
  • Backtick (``) and paren-style ($()) command substitions

Literal character escapes (backslash-escapes):

Outside of expansions:

  • ` may be printed in the output with \`
  • $ may be printed in the output with \$
  • " may be printed in the output with \"
  • \ may be printed in the output unescaped in most cases
  • If it is to be followed immediately by a `, a $, a ", a newline, or another \, it must be escaped with \\
  • All other characters will be printed normally

These follow the rules of a bash here-document

Inside expansions:

Example

This repository includes an example template and its output. Additionally, this gist includes the template I actually initially built this for.

Usage

but.sh <template-file> [-o|--output <output-file>] [[[-c|--config] <config-file>] ...]

About

A rather trusting bash template engine

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages