1

I have a number of variables that are being source'd from an external file:

my.sh

source my.conf

sed -i "s:var1:$var1:g" $SOME_FILE
sed -i "s:var2:$var2:g" $SOME_FILE
sed -i "s:var3:$var3:g" $SOME_FILE

my.conf

var1=abc:abc
var2=def!123
var3=&*(!"£

The script that source's these variables will run into issues if any of the values contain special characters (such as the colon character):

I have seen how variables can be individually sanitised, but is it possible to sanitise them all in one go? Note that the actual variable names do not have a pattern as in the example above.

Community
  • 1
  • 1
Chris Snow
  • 23,813
  • 35
  • 144
  • 309

1 Answers1

2

You could transform the configuration file before sourcing it so that the sed delimiter would be escaped:

source <(sed 's/:/\\\\:g' my.conf)

The <(command) form is referred to as Process Substitution.

devnull
  • 118,548
  • 33
  • 236
  • 227