0

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 [email protected] $recipient < message;

If the contents of message are:

"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.

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Right, so like envsubst < message | sendmail -f [email protected] $recipient.
envsubst exists in git bash but does not exist in cygwin bash.
Notice that EPOCHSECONDS is a special variable since Bash 5.0, and assignments to it are ignored.
0

Yes, it's possible, you can do this as follows:

#!/bin/bash
body=$( cat ~/body )

##### Set Message Variables ##########################################
EPOCHSECONDS=1662823014

##### Replace Template Variables #####################################
final_message=$( eval echo \"${body}\" )

##### Do whatever you want with ${final_message} #####################
echo "Final Message: "
echo "${final_message}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.