I'm writing a utility (which happens to be in python) which is generating output in the form of a TCL script. Given some arbitrary string variable (not unicode) in the python, I want to produce a TCL line like
set s something
... which will set TCL variable 's' to that exact string, regardless of what strange characters are in it. Without getting too weird, I don't want to make the output messier than needed. I believe a decent approach is
if the string is not empty and contains only alphanumerics, and some characters like
.-_(but definitely not$"{}\) then it can be used as-is;if it contains only printable characters and no double-quotes or curly braces (and does not end in backslash ) then simply put
{}around it;otherwise, put
""around it after using\escapes for"{}\$[], and\nnnescapes for non-printing characters.
Question: is that the full set of characters which need escaping inside double quotes? I can't find this in the docs. And did I miss something (I almost missed that strings for (2) can't end in \ for instance).
I know there are many other strings which can be quoted by {}, but it seems difficult to identify them easily. Also, it looks like non-printing characters (in particular, newline) are OK with (2) if you don't mind them being literally present in the TCL output.