6

As part of a system build script I have a script that creates various files and configurations.

However one part of the build script creates a new script that contains variables that I don't want resolved when the build script runs. Code snippet example

cat - > /etc/profile.d/mymotd.sh <<EOF
hostname=`uname -n`
echo -e "Hostname is $hostname"
EOF

I have tried all sorts of combinations of ' and " and ( and [ but I cannot get the script to send the content without substituting the values and placing the substitutes in the new script rather than the original text.

Ideas?

2 Answers 2

26

The easiest method, assuming you don't want anything to be substituted in the here doc, is to put the EOF marker in quotes, like this:

cat - > /etc/profile.d/mymotd.sh <<'EOF'
hostname=`uname -n`
echo -e "Hostname is $hostname"
EOF
Sign up to request clarification or add additional context in comments.

Comments

12

Easiest is to escape the $

echo -e "Hostname is \$hostname"

Comments

Your Answer

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