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

The concatenation of differently quoted command line arguments splits into multiple argv. #15888

Closed
5 tasks done
ArnoCan opened this issue Aug 8, 2021 · 6 comments
Closed
5 tasks done
Labels
Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a Resolution-Duplicate The issue is a duplicate.

Comments

@ArnoCan
Copy link

ArnoCan commented Aug 8, 2021

Prerequisites

Steps to reproduce

The concatenation of differently quoted string arguments for a command line call is resolved into separate argv entries.

For example a python one-liner, same for PowerShell scripts etc.
-> Call: C:\temp\PowerShell-7.1.3-win-x64> c:/Python371/python.exe -c "import sys;print(sys.argv[1:])" "a"'b'"c"
-> Result: ['a', 'b', 'c']

I would expect these strings to be actually concatenated into one string:
-> Result: ['abc']

As this is the case e.g. for:
-> Call: C:\temp\PowerShell-7.1.3-win-x64> c:/Python371/python.exe -c "import sys;print(sys.argv[1:])" "a""b""c"
-> Result: ['abc']

Expected behavior

PS C:\temp\PowerShell-7.1.3-win-x64> c:/Python371/python.exe  -c "import sys; print(sys.argv[1:])" "a"'b'"c"
['abc']

Actual behavior

PS C:\temp\PowerShell-7.1.3-win-x64> c:/Python371/python.exe  -c "import sys; print(sys.argv[1:])" "a"'b'"c"
['a', 'b', 'c']

Error details

No response

Environment data

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

Visuals

No response

@ArnoCan ArnoCan added the Needs-Triage The issue is new and needs to be triaged by a work group. label Aug 8, 2021
@vexx32
Copy link
Collaborator

vexx32 commented Aug 8, 2021

PS supports both single and double quoted string, so I'm not sure this is really unexpected.

Your example has three separate string tokens in it, so they get passed as three separate arguments.

PS's parsing treats " and ' as enclosing characters for strings, and it doesn't support concatenation like this. Three separate strings, three separate arguments.

@ArnoCan
Copy link
Author

ArnoCan commented Aug 9, 2021

See also #15889.
The reason I have to deal with this is that I am currently writing scanner/lexer/tokenizer for command line call strings - for raw input as well as the pre-scanned ARGV. This requires to process the strings exactly - for bash/sh/ksh + PowerShell + DOS/cmd.exe - each with it's own domain of course.

The differently quoted strings are scanned on the first view as three separate string tokens, but they miss a SEPARATOR. The "outer" quotes of a string are commonly dropped from a parsed command line string as these are temporary markers grouping the characters into a string. This would result in three concatenated strings with dropped temporary markers - the quotes. Thus as expected a single concatenated string argument - ARGV entry.

The same example for Posix based shells - bash/sh/ksh. for WSL/Linux:

C:\Users\test>wsl python -c "import sys;print(sys.argv[1:])" "a"'b'"c"
['abc']

C:\Users\test>

and for native Linux:

test:~$ python -c "import sys;print(sys.argv[1:])" "a"'b'"c"
['abc']
test:~$ 

@iSazonov iSazonov added Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a WG-Engine-ParameterBinder labels Aug 9, 2021
@iSazonov
Copy link
Collaborator

iSazonov commented Aug 9, 2021

/cc @mklement0

@rjmholt
Copy link
Collaborator

rjmholt commented Aug 12, 2021

Duplicate of #1995

@rjmholt rjmholt marked this as a duplicate of #1995 Aug 12, 2021
@ghost
Copy link

ghost commented Aug 13, 2021

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

@ghost ghost closed this as completed Aug 13, 2021
@mklement0
Copy link
Contributor

The problem is unrelated to #1995 - it equally affects PowerShell commands.

Instead, it is a specific instance of #6467 and - on Unix with potentially destructive consequences - #12647

The short of it is that composing a single string argument from a mix of quoting styles - the way you can do in POSIX-like shells such as bash - only works in PowerShell if the first string (or simple variable reference) is UNQUOTED:

# OK - single argument, because the first token is *unquoted*.
PS> Write-Output foo'bar'
foobar

# !! TWO arguments
PS> Write-Output 'bar'foo
bar
foo

More info in this SO answer.

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-Duplicate The issue is a duplicate.
Projects
None yet
Development

No branches or pull requests

5 participants