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.





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






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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

Node.js puppeteer - Use values from array in a loop to cycle through pages