1

I'm trying to write a script that updates a server config file to the latest format. The purpose of the script is to copy over existing (defined) variables to the latest config (in a temp file), then copy/overwrite the old config file with the newly generated one.

From my understanding, using double quotes will expand variables in sed. This seems to work fine when only one variable is used in the pattern:

sed -i "s|random|$PASSWORD|g" /tmp/latest_config

In the above example, the temp config file is successfully modified, and all instances of random are replaced with the value of $PASSWORD. However, this doesn't work the other way:

sed -i "s|$PASSWORD|random|g" /tmp/latest_config

In the above example, nothing happens, and no sed errors etc. The value of $PASSWORD in the temp config file is not modified...

NOTE: $PASSWORD in the first example is being sourced from "old" config file at the top of this script, but the goal of the second example would be replacing variable value...


I've seen dozens of similar "variable" questions on the SE network, however many of the answers are wrong (e.g. using single quotes, etc) or simply do not work for me. I have tried many formats, including single and double quotation marks, brackets (to differentiate environment variable from the variable being actually searched for by sed)... nothing has worked so far, e.g.:

sed -i "s|$PASSWORD|$PASSWORD|g" /tmp/latest_config
sed -i "s|$PASSWORD|${PASSWORD}|g" /tmp/latest_config
sed -i "s|\$PASSWORD|${PASSWORD}|g" /tmp/latest_config
sed -i 's|$PASSWORD|"${PASSWORD}"|g' /tmp/latest_config
sed -i 's|\$PASSWORD|"${PASSWORD}"|g' /tmp/latest_config

The only solution that works is a sort of hacky workaround:

sed -i "s|\(^PASSWORD=\).*|PASSWORD=\"$PASSWORD\"|g" /tmp/latest-config

Why? Do I need to export the existing variables first, or is this as good as it gets?...

  • The problem is likely that the shell expands $PASSWORD to whatever the password is, and then sed parses the resulting value as if it were a regex when in fact you want a literal string comparison. So sed's behaviour is going to depend on the contents of $PASSWORD and any non alpha numeric characters it contains. I recommend you use a template source file rather than trying to modify in place. – Colin Dec 11 '19 at 16:06
  • @Colin thanks for your suggestion. By chance, do you have a link or example of how I could proceed with a template source file (intermediate file?). Thanks! – Jesse Nickles Dec 12 '19 at 21:25
  • It's a fairly common way of automating configuration. And it's typically done this way for exactly the reasons you've described: https://stackoverflow.com/questions/8841195/how-can-i-script-file-generation-from-a-template-using-bash . Depending on how many servers you have to automate it may be worth putting the variables you're using in the config, into a database. – Colin Dec 16 '19 at 10:04
  • @Colin thanks for all this, I wish I could upvote your comments! – Jesse Nickles Dec 16 '19 at 17:40

0 Answers0