Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save roymanigley/8da5838688c513b82937350322d9ec64 to your computer and use it in GitHub Desktop.
Save roymanigley/8da5838688c513b82937350322d9ec64 to your computer and use it in GitHub Desktop.
replace placeholders in bash

To me this is the easiest and most powerful solution, you can even include other templates using the command eval echo "$(<template.txt):

Example with nested template

  1. create the template files, the variables are in regular bash syntax ${VARIABLE_NAME} or $VARIABLE_NAME

you have to escape special characters with \ in your templates otherwhise they will be interpreted by eval.

template.txt

Hello ${name}!
eval echo $(<nested-template.txt)

nested-template.txt

Nice to have you here ${name} :\)
  1. create source file

template.source

declare name=royman 
  1. parse the template
source template.source && eval echo "$(<template.txt)"
  1. the output
Hello royman!
Nice to have you here royman :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment