I currently use many heredocs in bash scripts.
These heredocs contain bash variables, e.g. ${MY_VAR}, which are substituted as the script executes and the heredoc is read.
Some of these heredocs have grown to hundreds of lines long, and so I would like to put these in their own files.
Unfortunately this breaks the variable substitution. I could add boilerplate to each file, turning each file into its own bash script (containing their own heredoc, which uses environment variables). This feels like pointless complexity.
Is there a way of making bash perform variable substitution on an external file as it is read in?
envsubstcommand.\${foo}Have I understood correctly?export. TryMY_VAR=$MY_VAR envsubst '${MY_VAR}' < heredoc.txt. For the escaping, you can try '\$MY_VAR' to differentiate$MY_VARfrom${MY_VAR}