Given cmd.exe
, I observed a special case which I do not understand, and which does not seemed to be explained by https://stackoverflow.com/a/4095133/16545605:
This simple command is executed as one would expect:
> type ..\..\..\..\..\..\windows\win.ini
; for 16-bit app support
[fonts]
[extensions]
[mci extensions]
[files]
[Mail]
MAPI=1
But what happens when done this way? Well, it opens Notepad with the given file and the shell is blocked until Notepad is closed again:
> cmd.exe /c "type ..\..\..\..\..\..\windows\win.ini"
On the other hand, those three variants behave as someone would expect (as the first example):
> cmd.exe /c type ..\..\..\..\..\..\windows\win.ini
...
> cmd.exe /c "type" "..\..\..\..\..\..\windows\win.ini"
...
> cmd.exe /c "type C:\windows\win.ini"
...
To open Notepad, it is also possible to just give the file name. It seems cmd.exe
opens the file with the default program if only the path is given:
> ..\..\..\..\..\..\windows\win.ini
> C:\windows\win.ini
So, it seems that cmd.exe
is interpreting the given command as two commands, and only "executes" the second one (opening the file, everything before is ignored). Furthermore, it seems the path has to be relative. My question now is, why, and under which circumstances this behavior happens. Please note, that type
is just an example. It also works with echo
, ping
, ipconfig
,...
If you already liked this behavior, lets add another one:
> cmd.exe /c "echo a > output.txt ..\..\..\..\..\..\windows\win.ini"
> type output.txt
a ..\..\..\..\..\..\windows\win.ini
If you do this without cmd.exe \c
, additionally a newline is introduced:
> echo a > output2.txt ..\..\..\..\..\..\windows\win.ini
> type output2.txt
a
..\..\..\..\..\..\windows\win.ini
As Linux user, I'm just baffled how unintuitive a shell can be. Is there some documentation why it is as it is, and how to prevent this behavior (without removing the cmd.exe /c
part)?