How shell parameters are parsed?
How shell parameters are parsed?
I used this simple script,named echo_arg in a msys shell
echo $1
echo $2
echo $3
For these 2 cases, called from CLI,
$ C:/msys/1.0/bin/sh.exe -c "C:/msys/1.0/bin/sh.exe echo_arg a "b" c"
$ C:/msys/1.0/bin/sh.exe -c "C:/msys/1.0/bin/sh.exe echo_arg a "b" c"
the output was
a
b
c
But CreateProcess function in a C program did not give the same result (compilation with mingw-w64, thread : win32, exception : seh, version : 7.3.0 )
#include <windows.h>
#include <stdio.h>
void main( int argc, char *argv )
int i;
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
char * cmd =
"C:/msys/1.0/bin/sh.exe -c "C:/msys/1.0/bin/sh.exe echo_arg a "b" c"",
"C:/msys/1.0/bin/sh.exe -c "C:/msys/1.0/bin/sh.exe echo_arg a \"b\" c"",
"C:/msys/1.0/bin/sh.exe -c "/bin/sh.exe echo_arg a \"b\" c"" ;
for (i=0;i<3;i++)
printf("t%sn",cmd[i]);
CreateProcess(NULL, cmd[i] , NULL,NULL,FALSE,0,NULL,NULL,&si,&pi);
WaitForSingleObject( pi.hProcess, INFINITE );
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
Ouput :
C:/msys/1.0/bin/sh.exe -c "C:/msys/1.0/bin/sh.exe echo_arg a "b" c"
a
b
c
C:/msys/1.0/bin/sh.exe -c "C:/msys/1.0/bin/sh.exe echo_arg a "b" c"
a
b c
C:/msys/1.0/bin/sh.exe -c "/bin/sh.exe echo_arg a "b" c"
a
b
c
Why, in the second case, b and c are not separated ($2="b c" and $3 is empty) ? And why case 3 worked.
Only for information, I am not a mad man that likes to play with " " / ... I have encountered this problem when I was trying to install GNU MP library (OS : windows 10). It did not work and it was the beginning of a long story before I found the cause.
Is it possible to know the CreateProcess parsing rules ? I read some but it does not explain the output difference when C: is added.
– Stef1611
Sep 5 at 17:27
Windows is mature and well supported product. It has documentation and forums (MSDN, Technet). If something does not work as documented, in your opinion, you're welcome to ask there.
– ddbug
Sep 7 at 12:29
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
CreateProcess has its own rules of parsing & matching quotes in command line. These do not necessarily are same as in posix sh. Suggestion: use c++ and raw strings to type backslashes as is.
– ddbug
Sep 4 at 1:12