BASH script to pass variables without substitution into new script

user964158 picture user964158 · Sep 26, 2011 · Viewed 8k times · Source

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?

Answer

Jonathan Callen picture Jonathan Callen · Sep 26, 2011

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