Repository navigation
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
Note: This has been reported before, but not as a dedicated issue:
- By Jordan Borean (@jborean93) in Different behavior on Linux vs. Windows for passing "*" to az cli #24170 (comment)
- Prior to that, by Andrew Hickman (@andrewhickman) in Add an option to disable globbing on Linux #17176 (comment)
On Unix, barewords passed as arguments to external (native) programs are subject to globbing (filename expansion), if they contain * or ?, for compatibility with native Unix shells.
-
In direct invocation, the way to avoid this is to quote the bareword, e.g.
'*' -
However - and this constitutes the bug - when splatting, the globbing cannot be prevented:
# On Unix-like platforms.
/bin/echo @( '*' )Note that using embedded quoting (e.g., @('"*"')) is not an option, because it becomes part of the argument.
Expected behavior
* should print verbatim.
Note:
-
The above assumes that
'*'should not be treated like a bareword in the context of splatting, which in turn implies that you fundamentally cannot specify barewords that way, and would have to use something like@((Get-Item * -Name))to manually perform the desired globbing. -
This strikes me as an acceptable trade-off and much preferable to the alternative: defaulting to interpretation as barewords, with a yet-to-be-devised opt-out method.
Actual behavior
The names of the files and subdirectories in the current directory print, because native globbing was applied.
Error details
No response
Environment data
PowerShell 7.5.0-preview.3 on Unix-like platforms.Visuals
No response
Activity
SteveL-MSFT commentedon Aug 26, 2024
The problem is that splatting doesn't differentiate literal strings currently. Is there a common enough problem that we should try to fix this (non-trivial that may break other things) or should we just document this as a limitation of splatting?
jborean93 commentedon Aug 26, 2024
My personal take is you can manually do the glob expansion if you wanted it but with the current logic you cannot use a literal
*in a splat so you should favour not expanding itI'm also not the biggest fan of Windows and non-WIndows having different behaviour here but ship has probably sailed.
mklement0 commentedon Aug 26, 2024
Steve Lee (@SteveL-MSFT), I agree with Jordan Borean (@jborean93).
To spell it out: a fix (technically a breaking change, but most likely bucket-3) is called for that simply disables native globbing via splatting.
theJasonHelmick commentedon Oct 16, 2024
The Interactive WG has reviewed this issue. This is not a splatting issue. This can be reproduced with a simpler example:
And
Bashhas the same behavior in this case:The WG would be interested in seeing more real-world examples of this problem before considering solutions.
jborean93 commentedon Oct 16, 2024
Jason Helmick (@theJasonHelmick) the issue here is specific to splatting as there is no way to turn off the automatic file globbing when using splatting. Unlike the non-splatting example where you can simply quote the `*‘ to avoid it.
jborean93 commentedon Oct 16, 2024
A more specific example, you can pass through a
*literal when using it as a positional argument.The problem that this issue is open for is there is no way (that I know off) to pass a
*literal when using splatting or using an array as a positional value. All the following will do the glob expansion and not pass through*as a literal charmklement0 commentedon Oct 17, 2024
Thanks, Jordan Borean (@jborean93). In a nutshell:
With an indirectly specified scalar argument, enclosure in
"..."is effective in suppressing globbing; applied to Jason Helmick (@theJasonHelmick)'s example:$a = '*'; /bin/echo "$a"With splatting (including implicit splatting via an array of positional arguments), there appears to be no way to suppress globbing, hence this issue.
The behavior with scalars is consistent with Bash, but not with Bash arrays, which can be double-quoted as a whole to signify that its elements should not be subject to globbing:
Obviously, in PowerShell this is not an option, given that enclosing an array reference in
"..."stringifies the whole array to form a single argument ($a = 'foo', '*'; printf '%s\n' "$a"yieldsfoo *as a single string argument).While native globbing is undoubtedly helpful, it is also awkward, in that selectively introduces an otherwise unknown concept to PowerShell: shell expansions, where the shell itself interprets arguments up front differently depending on whether or not they're quoted.
I don't think there's a good solution for the splatting case, hence the suggestion to simply disable native globbing when splatting (requiring the use of explicit
Get-Item/Get-ChildItemcalls instead if globbing is desired).As for the real-world impact:
It is awkward not to be able to use splatting whenever an argument that contains
*or?is required; while that is somewhat mitigated by patterns that match nothing being passed through as-is (e.g.,/bin/echo nosuch*), it makes for unpredictable behavior (what ends up getting passed depends on the names of the files and directories that happen to be present in the working directory).As for when you might want to pass
*or?(or arguments containing them) literally: say an external utility takes an arithmetic operator as an argument, or you need to pass a globbing pattern without expanding it up front, for later globbing in the context of an external utility (e.g., as infind . -iname '*.txt'; trying to implement this as$findArgs = '.', '-iname', '*.txt'; find @findArgsdoes not work properly, because the up-front expansion of*.txtcannot be prevented)Shayan-To commentedon Apr 22, 2025
This problem is a real pain for me. I use PowerShell a lot in linux (as my login shell when I own the machine), and this causes me a lot of headache and weird behavior when I least expect it.
And it is not something that you can see. You have a variable filled with some unknown value, you pass it to some command, and out comes the least expected results. And this becomes a very big problem in automated scripts.
One solution to this problem (at least for me) would be to add an option to disable globbing completely in linux.
kzrnm commentedon Oct 15, 2025
I’m facing exactly this issue in the git-completion-pwsh project I’m developing.
There’s no workaround when using wildcards as arguments.
https://github.com/kzrnm/git-completion-pwsh/blob/18b5d008bb6e43e85cb74c79084e165033c4928e/src/Common/GitCommands.ps1#L30
@argsis equivalent to$@in bash, but the lack of syntax equivalent to"$@"is problematic, in my opinion.Use VerbatimArgument
kzrnm commentedon Oct 16, 2025
It’s not a perfect solution, but adding the
--%verbatim argument can be used to avoid globbing.The drawback is that CMD-style environment variable expansion is performed, so it doesn’t behave like a Unix shell.
In the example below, the environment variable
OSTYPEis set tolinux-gnu.Note
Passing multiple
--%arguments is ignored altogether, which I believe is a bug. #26198Test script
Result
mklement0 commentedon Apr 15, 2026
Conceptually related:
SeeminglyScience commentedon Apr 22, 2026
Going to reopen to the interactive WG again, I think there might be more that needs to be discussed here
theJasonHelmick commentedon Apr 29, 2026
Michael Klement (@mklement0) and folks - thank you so much for keeping this alive. Also, thank you for gently correcting my incorrect comment earlier in the discussion :) We are considering the possibility of disabling native globbing for non-literal (or single quoted literal) arguments. Looking to get feedback on that idea, if anyone has objections please feel free to share.