Friday, March 12, 2010

Escaping Backticks within Backticks

If you have trouble with multiple backticks at command-line, it is because you aren't escaping them enough. It takes a lot more to escape then you might think at first. For example, in my last post I provided this example:
find `dirname \`dirname \\\`readlink -f \\\\\\\`which hg\\\\\\\`\\\`\`` -name mercurial
Here is another example:
dirname `dirname \`dirname \\\`dirname \\\\\\\`readlink -f \\\\\\\\\\\\\\\`which hg\\\\\\\\\\\\\\\`\\\\\\\`\\\`\``
So the pattern is that to escape you use 1, 3, 7, 15, 31, etc. where the next number of backticks required is the previous number doubled, plus 1. This can get out-of-hand quickly, so try not to escape backticks within backticks too much if you don't have to.

In addition, the level of backticks affects the number of backslashes required for escaping backslashes (example: Using sed to Escape Quotes).

No comments: