Skip to content

Instantly share code, notes, and snippets.

@caruccio
Last active August 3, 2016 15:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save caruccio/4288846 to your computer and use it in GitHub Desktop.
Save caruccio/4288846 to your computer and use it in GitHub Desktop.
Shell variable substitution from within a file.
##
# Just found the amazing builtin 'mapfile', perfect
# for substitution variable inside a file by its values.
##
mateus@mateus:/tmp$ cat input.txt
a = $a
b = $b
mateus@mateus:/tmp$ echo a=$a, b=$b
a=1, b=2
mateus@mateus:/tmp$ function subst() { eval echo -E "$2"; }
mateus@mateus:/tmp$ mapfile -c 1 -C subst < input.txt
a = 1
b = 2
##
# Simpler solution
##
mateus@mateus:/tmp$ EOF=EOF_$RANDOM; eval echo "\"$(cat <<$EOF
$(<input.txt)
$EOF
)\""
a = 1
b = 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment