def quote_cmdline(seq):
  """Quotes the strings in seq for feeding to shell.
  This is a severe protection to prevent:
  - variable, command, or other substitutions
  - shell expansions (parameter, wildcard)
  - word splitting
  - invocation of shell builtin (!!!)
  """
  # Compared to other implementations:
  # - Python 2.6's subprocess.py has list2cmdline, but I don't like it because
  #   it still allows the shell to interpret wildcards. We have to quote wildcards
  #   (*, [], {}, ?) and $ as well.
  # - Python 3.3 has shlex.quote that performs similarly.
  # The reverse operation can be done via shlex standard module.
  rslt = []
  for i in seq:
    inew = '"' + i.replace("\\", "\\\\").replace('"', '\\"').replace('$', '\\$').replace('`', '\\`') + '"'
    rslt.append(inew)
  return " ".join(rslt)