Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bash variable from within bash variable with text

I’m trying to echo a string into a file which has variables inside it along with plaintext. and use it later with a command but have the variable be interpreted. I have tried to use eval with limited success but then I cannot include characters such as <>

body=`cat ~/body`;

echo -e "${body}" >> message

message is later used in the following command

sendmail -f noreply@example.com $recipient < message;

If the contents of message are:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"Hello. The current epoch time is<br>
${EPOCHSECONDS}
"

I’d like the output to have the var interpreted and the body shown as

"Hello. The current epoch time is<br>
1662823014"

Is this possible? Thanks.

>Solution :

envsubst https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html should help you.

For example

$ cat message.txt
Hello. The current epoch time is
${EPOCHSECONDS}.

$ EPOCHSECONDS=1662823014

$ export EPOCHSECONDS

$ cat message.txt | envsubst
Hello. The current epoch time is
1662823014
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading