1) Special characters listed in cmd help
The following statement at the end of the cmd help (HELP CMD or CMD /?) used to really irk me.
I used to think "That is simply wrong! Characters like [ ] { and } are not special. How can Microsoft be so wrong?"cmd /? wrote: The special characters that require quotes are:
<space>
&()[]{}^=;!'+,`~
But then I read more carefully, and realized that the statement is related to file/directory completion via Ctrl-F and Ctrl-D
If file or directory completion is used, then any file/directory name that includes any of the special characters will be quoted.cmd /? wrote: If completion is enabled with the /F:ON switch, the two control
characters used are Ctrl-D for directory name completion and Ctrl-F for
file name completion. To disable a particular completion character in
the registry, use the value for space (0x20) as it is not a valid
control character.
Completion is invoked when you type either of the two control
characters. The completion function takes the path string to the left
of the cursor appends a wild card character to it if none is already
present and builds up a list of paths that match. It then displays the
first matching path. If no paths match, it just beeps and leaves the
display alone. Thereafter, repeated pressing of the same control
character will cycle through the list of matching paths. Pressing the
Shift key with the control character will move through the list
backwards. If you edit the line in any way and press the control
character again, the saved list of matching paths is discarded and a new
one generated. The same occurs if you switch between file and directory
name completion. The only difference between the two control characters
is the file completion character matches both file and directory names,
while the directory completion character only matches directory names.
If file completion is used on any of the built in directory commands
(CD, MD or RD) then directory completion is assumed.
The completion code deals correctly with file names that contain spaces
or other special characters by placing quotes around the matching path.
Also, if you back up, then invoke completion from within a line, the
text to the right of the cursor at the point completion was invoked is
discarded.
The special characters that require quotes are:
<space>
&()[]{}^=;!'+,`~
But of course Microsoft still is not quite correct - they forgot to list % as one of the special characters that requires quoting.
2) Escaping redirection file handles
It is fairly common knowledge within this community what will happen with the following:
Code: Select all
echo We will meet at 2>output.txt
It is also fairly common knowledge that the solution is simple, move the redirection to the front (or anywhere where there is no chance of ambiguity)
Code: Select all
>output.txt echo We will meet at 2
Code: Select all
echo We will meet at ^2>output.txt
3) It is impossible to redirect to a file with a name beginning with &
Hopefully we all know what happens with the following:
Code: Select all
echo Hello world >&2
None of the following work - they all write the output to stderr instead of a file.
Code: Select all
echo Hello world >^&2
echo Hello world >"&2"
set "file=&2"
setlocal enableDelayedExpansion
echo Hello world >"!file!"
It doesn't even work if there is no digit after the &
Code: Select all
echo Hello world >"&test.txt"
Code: Select all
The handle could not be duplicated
during redirection of handle 1.
I know jeb makes it a habit of disproving absolute claims like this, but I am pretty confident it is not possible to redirect to a file name beginning with & using standard cmd.exe commands (disregarding the possible MKLINK solution). Challenge on
Another interesting find - all text after &2 is simply ignored up until the first unquoted token delimiter.
Code: Select all
echo Hello world >"&2 this is ignored"and_so_is_this but_this_is_not_ignored
Code: Select all
Hello world but_this_is_not_ignored