I want to replace the content of a file
cat test.sh
attribute = "metaapp"so I am trying to replace the metaapp with string ${meta.app} with hep of sed using
sed -i s/metaapp/${meta.app}/g test.shand getting bad substitution error . How to use sed here so the output will looks like this
attribute = "${meta.app}" 0 1 Answer
It's not about sed, it's about the shell.
${meta.app} is almost like the syntax for the parameter substitution (other names: variable substitution, variable expansion): ${var}. Variable names cannot contain dots. There is syntax like ${var-foo} where - does not belong to the name either, there are more of them; but ${var.foo} or ${meta.app} is not defined.
That's why your shell complained about bad substitution. sed was not even started. Try mumblemumble ${meta.app} and the error will be the same. You won't see an error saying the mumblemumble command was not found; the shell won't get this far.
To fix, single-quote the entire sed expression:
sed -i 's/metaapp/${meta.app}/g' test.shStrings in single-quotes are not expanded by the shell. You can achieve the same result by escaping troublesome characters, while double-quoting or not quoting at all (s/metaapp/\${meta.app}/g). But you need to know what characters to escape in which case. With single-quotes you easily protect the entire string.
That's how you move the shell out of the way. It would not be so easy if you needed a literal ' inside the string, or any kind of substitution from the shell. Fortunately you don't, so single-quoting is convenient and the right thing to do. Now you only need to care how sed interprets the string.