environment variables in a sed script file

Hello Everyone

I need to create a script file which must append some lines to a target text file, I'm using sed for windows, the script file look like this:

{
a\
STRINGTABLE  DISCARDABLE\
BEGIN\
5, 150 {a\
  #define RC_SHELL,  "%ID_SHELL%"\
  #define RC_NAME, "%ID_NAME%"\
END
}

However the environment variables (ID_SHELL and ID_NAME) is not being expanded, how can I expand those variables?

The only workable solution is a two-pass one. Use the sed file as you have created it (removing the comma and double quotes around your variable), then make a second pass with sed on the command line...

sed -i 's/%ID_SHELL%/'"%ID_SHELL%"/; s/%ID_NAME%/'"%ID_NAME"/'
1 Like