3 ms·
What always confused me on Windows is how shells (e.g. CMD) fit into all of this. You can use pipes, variables, semicolon delimiters etc inside a line, which ha
by xg15 5y ago
What always confused me on Windows is how shells (e.g. CMD) fit into all of this. You can use pipes, variables, semicolon delimiters etc inside a line, which have to be parsed by the shell before launching any process - and those interact with quoting!
So does that mean the shell first parses a line using its own quoting rules, then launches the processes which parse the line again, possibly using a different set of quoting rules?
This sounds plainly insane.
- formerly_proven 5y agoIIRC Batch interprets ^ escapes unless it's double quoted - so, yes.
- mananaysiempre 5y agoIt also has a different idea of what’s actually double-quoted: it ignores backslashes completely. (Well, the Microsoft C runtime and SHELL32.DLL also have different ideas in that regard, as mentioned in the links in the post, but CMD.EXE’s different different idea is ... more different. I still don’t understand whetehr SHELL32.DLL and pre-2008 C runtime are the same or not. An MSDN blog post discussing this is—concisely if not in detail—here: https://docs.microsoft.com/en-gb/archive/blogs/twistylittlepassagesallalike/everyone-quotes-command-line-arguments-the-wrong-way https://docs.microsoft.com/en-gb/archive/blogs/twistylittlep...; I think I came upon it by following links from the head post but I can’t seem to recover the path.)
- ChrisSD 5y agoYes the shell interprets it first. `cmd.exe` does very little processing and mostly passes on the literal string. But what it does do can be confusing (e.g. `^` is an escape character which is annoying if you were trying to pass in a regex). In powershell you can use `--%` to pass a literal string as the command line. This is useful for applications that don't use the standard C/C++ parsing rules. See: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing?view=powershell-7.2#the-stop-parsing-token https://docs.microsoft.com/en-us/powershell/module/microsoft...
- dundarious 5y agoThank you very much for pointing out --%. I often find it incredibly hard to find what I'm looking for from Microsoft's voluminous tutorial-style Powershell docs.
- rossy 5y agoYep, which leads to things like the following not creating out.txt (in cmd.exe) prog.exe "abc \" def" > out.txt