0

I created a sed function to escape slashes.

sed_replace_word() {
  var_1_clean=$(echo "$1" | sed 's/\//\\\//g')
  var_2_clean=$(echo "$2" | sed 's/\//\\\//g')
  sed -i "s/$var_1_clean/$var_2_clean/g" $3 
}

When I'm now calling the function, he doesn't use the variables, because they need to set in single quotes:

sed_replace_word "^EXT_IF=.*" "EXT_IF="${INTERFACE}"" "/etc/arno-iptables-firewall/firewall.conf"

The Output of that example should be:

EXT_IF="eth0"

Is it possible to set single quotes (or to make the script use the variables), without setting the ' single quotes manually? Maybe an option to set them in the function, if there is a variable "detected".

Yes, I want to call a function and the parameter I'm giving to the function may contain a variable.

The content of that variable should be available in that function. So ${INTERFACE} (which contains eth0 for exmaple) should be in var_2_clean.

Example:

INTERFACE="eth0"

Calling function (shortended):

sed_replace_word "EXT_IF=another_content + ${INTERFACE}"

Result at the end of the function it should do:

sed -i "s/$var_1_clean/EXT_IF=another_content + eth0/g" $3
TylerH
  • 19,065
  • 49
  • 65
  • 86
Aeris
  • 149
  • 2
  • 16
  • 1) don't attempt to use sed to insert escape sequences into a string to make it "clean". 2) If you ignore the first item, `sed 's@/@\\/@g'` – William Pursell Apr 01 '20 at 11:34
  • I'm pretty sure the linked question is not a duplicate, as this is asking about embedding a double quote in the value assigned to the 2nd argument passed to the function, but the question is not clear. I think you're looking for `sed_replace_word "^EXT_IF=.*" "EXT_IF=\"${INTERFACE}\"" ...`. But it's not at all clear. – William Pursell Apr 01 '20 at 11:37
  • Edit: Updated Question! – Aeris Apr 01 '20 at 13:59

0 Answers0