I want to escape back quotes in a variable before writing it to a file. Unfortunately some of these lines have some disagreeable items.
(I want to avoid foo from expanding. I want foo="ModPagespeedLoadFromFileRuleMatch disallow .*" to be taken literally and substitution run on it. I do not want any expansion. It is just a directive to be placed in a file. )
foo="ModPagespeedLoadFromFileRuleMatch disallow .*"
echo ${foo//\\/\\\\}
This gives me:
ModPagespeedLoadFromFileRuleMatch disallow . .. .bash_history .bashrc .cloud-locale-test.skip .filemin .gnupg .local .mysql_history .profile .rnd .rpmdb .ssh
I also tried like this:
foo='ModPagespeedLoadFromFileRuleMatch disallow .*';
bar=$( printf "$foo" | sed 's/\\/\\\\/g' );
echo $bar
foo='ModPagespeedLoadFromFileRuleMatch disallow .*';
bar=$( echo "$foo" | sed 's/\\/\\\\/g' );
echo $bar
Same problem.
What am I missing here?
PS. Try this:
foo='ModPagespeedLoadFromFileRuleMatch disallow .*'
echo "$foo"
Then try this (I do not want this to happen when piping):
foo='ModPagespeedLoadFromFileRuleMatch disallow .*'
echo "$foo" |
Note the pipe in the second example
ANSWER:
foo="ModPagespeedLoadFromFileRuleMatch\ disallow .*"
echo "${foo//\\/\\\\}"
foo='ModPagespeedLoadFromFileRuleMatch disallow .*';
bar=$( printf "$foo" | sed 's/\\/\\\\/g' )
echo "$bar"
foo='ModPagespeedLoadFromFileRuleMatch disallow .*';
bar=$( echo "$foo" | sed 's/\\/\\\\/g' );
echo "$bar"
echo $bar
. You need quoting of$bar
(and all other expansions). – Kusalananda May 23 '19 at 10:47Can you fix it?
– conanDrum May 23 '19 at 10:49$bar
.echo "$bar"
. – Kusalananda May 23 '19 at 10:51