Skip to content

Instantly share code, notes, and snippets.

@rindeal
Last active August 29, 2015 14:11
Show Gist options
  • Save rindeal/ad999f8a267c2eeff444 to your computer and use it in GitHub Desktop.
Save rindeal/ad999f8a267c2eeff444 to your computer and use it in GitHub Desktop.
pure BASH template processor
#!/bin/bash
# http://stackoverflow.com/a/2916159/2566213
PATTERN=(.*)(\$\{([a-zA-Z_][a-zA-Z_0-9]*)\})(.*)
line="$(cat; echo -n a)"
end_offset=${#line}
while [[ "${line:0:$end_offset}" =~ $PATTERN ]] ; do
PRE="${BASH_REMATCH[1]}"
POST="${BASH_REMATCH[4]}${line:$end_offset:${#line}}"
VARNAME="${BASH_REMATCH[3]}"
eval 'VARVAL="$'$VARNAME'"'
line="$PRE$VARVAL$POST"
end_offset=${#PRE}
done
echo -n "${line:0:-1}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment