Embed this Perl snippet in your bash scripts to configure files based on environment variables!

#!/usr/bin/env bash
# Copyright 2022 Dario Balboni
# Code released under MIT License
#
perl -pe '
sub b64($) {
  my $bitstring = unpack("B*", $_[0]);
  my @sixs = unpack("(A6)*", $bitstring);
  my $padding = ((6 - (length $sixs[-1]) % 6) % 6);
  $sixs[-1] = join "", ($sixs[-1], "0" x $padding);
  my @enc = ("A".."Z", "a".."z", "0".."9", "+", "/");
  @sixs = map { unpack("c", pack("b6", join "", reverse(split "", $_))) } @sixs;
  my @s = map { $enc[$_] } @sixs;
  join "", (@s, "=" x ($padding / 2));
}
my $a = join("",<>) =~ s{\$\{(.*?)(\:-(.*?))?\}}{$ENV{$1}//$3//die "ENVVAR $1 NOT FOUND"}ger;
while ($a =~ m/\{\@((.|\s|\n)*?)\@\}/) {
  $a = $a =~ s{\{\@(((?!\{\@|\@\})(.|\s|\n))*)\@\}}{b64($1)}ger;
}
print $a;
' < input.yaml > output.yaml

The script first substitutes all environment variables having the syntax ${VARNAME} or ${VARNAME:-DEFAULT}, then recursively substitutes all fragments {@inner@} with the base64 encoded representation of inner: base64(inner).

Example input and output files:

# input.yaml
unavailable: ${UNAVAILABLE:-N/A}
shell: ${SHELL}
encoded: {@{ "shell": "${SHELL}" }@}
# output.yaml
unavailable: N/A
shell: /bin/bash
encoded: eyAic2hlbGwiOiAiL2Jpbi9iYXNoIiB9

Pretty cool, huh?