The Windows command prompt (cmd.exe
) has an optional /s
parameter, which modifies the behavior of /c
(run a particular command and then exit) or /k
(run a particular command and then show a shell prompt). This /s
parameter evidently has something to do with some arcane quote handling.
The docs are confusing, but as far as I can tell, when you do cmd /c
something
, and the something
contains quotation marks, then by default cmd
will sometimes strip off those quotes, and /s
tells it to leave them alone.
What I don't understand is when the quote removal would break anything, because that's the only time /s
("suppress the default quote-removal behavior") would be necessary. It only removes quotes under a certain arcane set of conditions, and one of those conditions is that the first character after the /c
must be a quotation mark. So it's not removing quotes around arguments; it's either removing quotes around the path to the EXE you're running, or around the entire command line (or possibly around the first half of the command line, which would be bizarre).
cmd /c "c:\tools\foo.exe" arg1 arg2
, then quotes are unnecessary, and if cmd
wants to remove them, fine. (It won't remove them if the path has a space in the name -- that's another of the arcane rules.) I can't imagine any reason to suppress the quote removal, so /s
seems unnecessary.cmd /c "foo.exe arg1 arg2"
, then it seems like quote removal would be a necessity, since there's no EXE named foo.exe arg1 arg2
on the system; so it seems like opting out of quote removal using /s
would actually break things. (In actual fact, however, it does not break things: cmd /s /c "foo.exe arg1 arg2"
works just fine.)Is there some subtlety to /s
that's eluding me? When would it ever be necessary? When would it even make any difference?
The Windows command prompt ( cmd.exe ) has an optional /s parameter, which modifies the behavior of /c (run a particular command and then exit) or /k (run a particular command and then show a shell prompt).
The Command Prompt, officially called the Windows Command Processor and often abbreviated to CMD, is the command line interface for Windows operating systems. A command line interface is a way of interacting with a computer directly using text commands.
greater control of an OS or application; faster management of many operating systems; ability to store scripts to automate regular tasks; basic command-line interface knowledge to help with troubleshooting, such as network connection issues.
You Gain Greater Control over System Functions Perhaps the most obvious reason to learn the command line is for its original function: greater control. There are commands accessible only via shell that can control very complex operations on Unix/Linux and Windows machines.
Cmd /S is very useful as it saves you having to worry about "quoting quotes". Recall that the /C
argument means "execute this command as if I had typed it at the prompt, then quit".
So if you have a complicated command which you want to pass to CMD.exe you either have to remember CMD's argument quoting rules, and properly escape all of the quotes, or use /S
, which triggers a special non-parsing rule of "Strip first and last "
and treat all other characters as the command to execute unchanged".
You would use it where you want to take advantage of the capabilities of the CMD shell, rather than directly calling another program. For example environment variable expansion, output or input redirection, or using CMD.exe built-ins.
Example:
Use a shell built-in: This executes as-if you had typed DEL /Q/S "%TMP%\TestFile"
at the prompt:
CMD.exe /S /C " DEL /Q/S "%TMP%\TestFile" "
This executes SomeCommand.exe redirecting standard output to a temp file and standard error to the same place:
CMD.exe /S /C " "%UserProfile%\SomeCommand.exe" > "%TMP%\TestOutput.txt" 2>&1 "
So what does /S
give you extra? Mainly it saves you from having to worry about quoting the quotes. It also helps where you are unsure whether for example an environtment variable contains quote characters. Just say /S
and put an extra quote at the beginning and end.
Vaguely Related: $* in Bourne Shell.
Some background
Recall that the list of arguments to main() is a C-ism and Unix-ism. The Unix/Linux shell (e.g. Bourne Shell etc) interprets the command line, un-quotes the arguments, expands wildcards like *
to lists of files, and passes a list of arguments to the called program.
So if you say:
$ vi *.txt
The vi command sees for example these arguments:
vi a.txt b.txt c.txt d.txt
This is because unix/linux operates internally on the basis of "list of arguments".
Windows, which derives ultimately from CP/M and VAX, does not use this system internally. To the operating system, the command line is just a single string of characters. It is the responsibility of the called program to interpret the command line, expand file globs (*
etc) and deal with unquoting quoted arguments.
So the arguments expected by C, have to be hacked up by the C runtime library. The operating system only supplies a single string with the arguments in, and if your language is not C (or even if it is) it may not be interpreted as space-separated arguments quoted according to shell rules, but as something completely different.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With