Skip to main content
2 of 5
add awk solution
user1404316
  • 3.1k
  • 15
  • 23

What I got working is:

domain=aa ; eval sed s"/\\\$domain/$domain/" ~/nginx_app >> ${domain}.conf

Let me know. If it works for you, mark the answer as accepted.

What is happening is that eval is a bash built-in command that evaluates all its arguments and then evaluates them as a command. Thus, the second $domain of the sed command expands into aa in my test case. However, since the first $domain has its $ identifier escaped (three required within sed - would take a longer explanation, if you need, ask) it is NOT expanded.

There exists another command-line program called awk which has a more powerful, direct, and prettier way of passing a variable from outside it to inside it, but sed is considered faster an more efficient (not sure if really true nowadays), so that's why I answered with sed because this was a simple case. In awk, you could use:

awk -v domain=aa '{gsub("\\$domain",domain);print}' ~/nginx_app >> ${domain}.conf
user1404316
  • 3.1k
  • 15
  • 23