3 ms·
On Unix, most tools don't really need to use the shell at all; it's enough to treat argument lists as lists internally and pass them to exec or posix_spawn (whi
by comex 10y ago
On Unix, most tools don't really need to use the shell at all; it's enough to treat argument lists as lists internally and pass them to exec or posix_spawn (which of course, unlike the Windows _exec and _spawn, aren't broken for arguments with spaces!).
However, at minimum it is still useful to shell-escape arguments when displaying them to the user (for ease of copy+paste), so it's unfortunate that many languages don't have any standard library function to do this - including C on POSIX, and Python before version 3.
- iokanuon 10y agoBut in comparasion with MS systems, escaping strings for unix shells is very simple - just prepend and append an ' and change every ' into '\''
- ajross 10y agoAlas, no. Bourne shell syntax allows for double quotes with variable interpolation and some other fancy syntax (including backslash escaping of literal double-quote characters), and a single-quote syntax for "raw" strings with no fancy syntax INCLUDING backslash escaping. So your rule won't work. You can't single-quote a string that itself contains single quotes, which makes for some fun when you have arbitrary strings (file names are the big frustration) that need to be substituted into a parseable command line. But like I said above: Bourne syntax[1] is only one kind of insanity, which is still much better than the DOS/Windows world of a separate parser for every app. [1] We shall not speak of the C shell here.
- skissane 10y agoA method I've used: replace all occurrences of ' with '"'"' and then surround resulting string with ' It's a bit ugly but it works.
- ufo 10y agoTheir rule does work. The single quotes are transformed into '\''. The first ' closes the current single quotes string. The \' adds a single quote character outside a quoted string (and outside single quotes you can use backslash escapes). Finally, the last ' reopens the single quoted string that we closed before.
- d0mine 10y agopipes.quote() is in Python's stdlib since forever. Though it was not documented until it became shlex.quote() in Python 3.
- kazinator 10y ago_exec an _spawn are not Windows; they are functions in the MS Visual C redistributable run-time. (Well, they are also in a system DLL called MSVCRT.DLL. That is an internal library which is undocumented and considered by Microsoft to be off-limits to applications.)