My bash script builds a string variable $arrayLMEs
, containing a string like:
var availableTags=[ "01 East Bering Sea", "02 Gulf of Alaska"];
I need to put this string in a javascript code, to replace a placeholder. I was thinking to use something like:
perl -i -pe 's/PLACEHOLDER/'"${arrayLMEs}"'/' filename
But actually the command complains, because of the double quotes found in the string, which are messing up the bash command and in consequence the perl command.
How can I fix the command to keep the spaces and double quotes?
Use the
-s
switch to pass the variable to perl:The
--
signifies the end of the command line arguments.-var="$arrayLMEs"
sets a perl variable with the value of your shell variable$arrayLMEs
.Alternatively, you could use awk:
A nice side-effect of using awk is the replacement is a simple string, so metacharacters in the original string won't be interpreted.