Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When calling cmd script, comma in string is not preserved #11295

Closed
jiasli opened this issue Dec 9, 2019 · 2 comments
Closed

When calling cmd script, comma in string is not preserved #11295

jiasli opened this issue Dec 9, 2019 · 2 comments
Labels
Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a Resolution-Answered The question is answered.

Comments

@jiasli
Copy link

jiasli commented Dec 9, 2019

Steps to reproduce

Create a batch file mybatch.cmd containing:

@echo off
echo %1
echo %2

In PowerShell call it with

PS C:\Users\me> .\mybatch.cmd "a,b"
a
b
PS C:\Users\me> .\mybatch.cmd 'a,b'
a
b
PS C:\Users\me> .\mybatch.cmd --% "a,b"
"a,b"
ECHO is off.  # this is expected in cmd
PS C:\Users\me> .\mybatch.cmd --% 'a,b'
'a
b'            # this is expected in cmd

Expected behavior

Comma inside the string should be preserved, like when calling it in cmd:

mybatch.cmd "a,b"
"a,b"
ECHO is off.

Actual behavior

Comma is parsed as array.

Environment data

$PSVersionTable 

Name                           Value
----                           -----
PSVersion                      6.2.3
PSEdition                      Core
GitCommitId                    6.2.3
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
$PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.0.0-preview.6
PSEdition                      Core
GitCommitId                    7.0.0-preview.6
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
@jiasli jiasli added the Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a label Dec 9, 2019
@mklement0
Copy link
Contributor

It isn't PowerShell that parses the argument as an array, it is cmd that interprets unquoted token a,b as two arguments, because not only spaces but also , and ; can serve as argument separators.

PowerShell performs re-quoting as needed behind the scenes, and in the absence of spaces inside arguments, it currently decides that enclosing the value in double quotes is not necessary, so that both .\mybatch.cmd "a,b" and .\mybatch.cmd 'a,b' end up getting called as .\mybatch.cmd a,b, at which point cmd considers a and b separate arguments.

This re-quoting logic does not special-case calling cmd.exe or batch files, and is currently generally broken - see #1995

The workaround is to use embedded double quoting:

.\mybatch.cmd '"a,b"'
.\mybatch.cmd "`"a,b`""

You can use the iep function defined here to automate this quoting and to compensate for the other ways in which re-quoting is broken.

Note that the planned fix for #1995, detailed in this RFC, will break the workaround (as it will break all other current workarounds) and - unless cmd.exe / batch-file calls are special-cased - would offer no alternative (other than the limited --% approach) - see this comment.

@iSazonov iSazonov added the Resolution-Answered The question is answered. label Dec 9, 2019
@ghost
Copy link

ghost commented Dec 11, 2019

This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a Resolution-Answered The question is answered.
Projects
None yet
Development

No branches or pull requests

3 participants