#!/bin/bash
s="TEST=bla
TEST_2=blabla
TEST=bla4"
t="This is some fake config file. The value of test variable is %TEST%."

repl="$(sed -n 's/^TEST=\(.*\)/\1/p;q' <<< "$s")"
sed "s|%TEST%|$repl|g" <<< "$t"

sed "s|%TEST%|$(sed -n 's/^TEST=\(.*\)/\1/p;q' <<< "$s")|g" <<< "$t"