I have a list of strings and I want to pass these strings as arguments in a single Windows command line call. For simple alphanumeric strings, just pass them verbatim:
> script.pl foo bar baz yes no foo bar baz yes no
I understand that if an argument contains spaces or double quotes, I need a backslash - avoid double quotes and backslashes, and then specify the argument twice.
> script.pl foo bar baz "\"yes\"\\\"no\"" foo bar baz "yes"\"no"
But when I try to pass an argument with literal percent signs, this happens:
> script.pl %PATH% C:\Program Files\PHP\;C:\spaceless\perl\bin\;C:\Program Files\IBM\Java60\bin; (...etc.)
Double quoting does not work:
> script.pl "%PATH%" C:\Program Files\PHP\;C:\spaceless\perl\bin\;C:\Program Files\IBM\Java60\bin; (...etc.)
The backslash does not work (note how the backslash is present in the output):
> script.pl \%PATH\% \%PATH\%
In addition, the rules are not compatible for backslashes with backslashes:
> script.pl "\\yes\\" \\yes\ > script.pl "\yes\\" \yes\ > script.pl "\yes\" \yes"
In addition, there are special characters on the Windows command line command line, as in all shells. What is the general procedure for safely escaping arbitrary command line arguments for use on a Windows command line?
The ideal answer will describe the escape() function, which can be used in the following situations (Perl example):
$cmd = join " ", map { escape($_); } @args;
Here are some examples of strings that should be safely escaped by this function (I know some of them look like Unix-like, intentional):
yes no child.exe argument 1 Hello, world Hello"world \some\path with\spaces C:\Program Files\ she said, "you had me at hello" argument"2 \some\directory with\spaces\ " \ \\ \\\ \\\\ \\\\\ "\ "\T "\\T !1 !A "!\/'" "Jeff's!" $PATH %PATH% & <>|&^ ()%!^"<>&| >\\.\nul malicious argument"&whoami *@ $$A$@