I was hoping to use bash to loop through a file and turn all placeholder varialbes into real variables:
- $PLACEHOLDER_USER -> $USER
- $PLACEHOLDER_STATE -> $STATE
It needs to work with any variable starting with placeholder and turn it into its real variable. This is the code I have so far:
$FILE="/mytest.conf"
sed -i "s/$var1/$var2/g" "$FILE"
However I'm not sure how I make it loop through the entire file, and I'm not sure how I can make it with any variable which starts with $PLACEHOLDER_
.
The default action of
sed
is to read and print every line of the input file. You can modify this behavior in various ways by writing ased
script. A typical script would be something likePay attention to the quoting here; the double quotes allow the shell to replace
$USER
and$STATE
with their values from the environment, while the backslashed dollar signs will not be substituted. So the shell performs some substitutions, and by the timesed
actually runs, the script has become(I supplied single quotes here to emphasize that no further substitution will take place.)
In the more general case,
sed
has no access to your environment variables, but you can write a shell script which generates ased
script from your variables.This is somewhat tricky. The output from the first
sed
script is anothersed
script which is read by anothersed
instance withsed -f -
. (This is not supported on all platforms, but should at least work on Linux. If yours does not support this, you can work around it by writing the script to a temporary file, or using a command substitution.)env
lists your environment variables. The firstsed
script will prefix each variable name with\$PLACEHOLDER_
and generate a snippet ofsed
to replace any occurrence of that with its value from the environment. (Caveat: If the value contains regex metacharacters, you will need a significantly more complex script. If the values can contain slashes, you need to replace the slash delimiter with another delimiter; you can use any ASCII character you like.)