# Variable expansion in a "here" file

This topic is 3730 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm using a here file which includes a variable defined in the script which does expand as expected when the code for the here file is inline in the script. When I cut the code out of the script and paste it into its own file to be used as the here file, the variable does not expand. Since I only use the one variable and it happens to be in the last line of the here file, my current workaround is to still use the separate file but leave this last line out of it, and in the script use the syntax for an inline here file consisting of two lines: cat herefile dosomethingwith $MYVAR Is there any way to make this work with that last line inside the external herefile? I realize that catting a here file is not how it's designed to work, but as far as my workaround goes, if I used the external here file as designed (redirect the contents of the file as input to the program using the here file), I wouldn't be able to include my last line - the here file input would end at the end of the external file. #### Share this post ##### Link to post ##### Share on other sites Advertisement Err, I'm sure I doesn't understand your question totally ;) Do you mean that the definition of a variable in one script file B isn't visible in another script file A although B is executed by A? That behaviour would be correct, since executing a shell script is done in an own shell with its own environment. That is like using the "exec" command (it is named "exec" at least by the bash). One can avoid using an own shell by not executing but "sourcing" B inside A. For the bash, the syntax would be "source B". Then B would use the same environment as A does, and any variable defined in B would be shared with A. Is that what you're asking for? #### Share this post ##### Link to post ##### Share on other sites No, a "here" file is not a script, it's a way of sending automated input to an interactive program within a script. This input can either be within the script itself or read from another file. When it's in the script, the value of my variable is used, which is what I want as far as my variable is concerned. When it is read from a file, which is the method I would prefer to use, the name of my variable is used. #### Share this post ##### Link to post ##### Share on other sites When you switch the input data from a here document to an external diskfile it's not longer a here document Here's what you need to do. (1) Replace the variable, say$MYVAR, with a substitution token in your data file. For instance, "@@MYVAR@@".

(2) Run you data file through a substitution filter like, for instance, sed.

  fudge <-EOF    This is a here document.    It's got a variable $MYVAL EOF you would instead do this.  sed -e "s,@@MYVAR@@,$MYVAR," mydatafile | fudge
Where "fudge" is your program previously consuming your herefile. This moves the variable substitution from the here document into the sed command line.

Bingo.

--smw

##### Share on other sites
Quote:
 Original post by BregmaWhen you switch the input data from a here document to an external diskfile it's not longer a here document

Could here documents be any more misleadingly named? The name implies a stand-alone external file, but when you actually do this it is no longer technically a here file/document? It's only a true here file when it exists as one portion of a larger file? Bah. What do you call this, then, inside a script?

ftp someserver.com <herefile

Simply input redirection?

Quote:

--smw

• 37
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631360
• Total Posts
2999556
×