I am trying to use a user-defined global variable in a cat <<eof>> filename situation, but it's not working. I found what I want in simple cat << eof examples on this forum, which led me here: http://www.tldp.org/LDP/abs/html/here-docs.html. But I would like to to something like the following, with the added >> and filename (This does not work.):


#after testing I will define this in .bashrc or somewhere?
set IP_ADDR = ""

#empty the file in case there is something in it
truncate -s 0 sample.conf

# open a file and append
cat <<EOF >> sample.conf
  This is my sample config file
  Server IP = $IP_ADDR
  this is the last line of my sample config file

#print the file to verify
cat sample.conf

This produces a null string after Server IP =

sed would work if I already had the file written except for the missing variable value, but I won't necessarily have the IP address (or whatever) readily available as a literal string.

Also, I might not have the literal string available for use as a command line parameter when I run the script. $1 does work within the cat << eof >> filename code block. I proved that. But I won't have immediate access to the value when I start this script...

set IP_ADDR = ""

is incorrect. You need:


(without spaces).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.