Skip to content

Unix: Native globbing cannot be bypassed when using splatting #24178

Open

Description

Prerequisites

Steps to reproduce

Note: This has been reported before, but not as a dedicated issue:

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

  1. added
    Needs-Triage
    on Aug 20, 2024
  2. SteveL-MSFT commented on Aug 26, 2024

    @SteveL-MSFT
    Member

    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?

  3. jborean93 commented on Aug 26, 2024

    @jborean93
    Collaborator

    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 it

    $myArgs = @('--files'; (Get-ChildItem $pwd).Name)
    my_bin @myArgs

    I'm also not the biggest fan of Windows and non-WIndows having different behaviour here but ship has probably sailed.

  4. mklement0 commented on Aug 26, 2024

    @mklement0
    ContributorAuthor

    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.

  5. added
    WG-Interactive-Console
    WG-NeedsReview
    on Aug 26, 2024
  6. theJasonHelmick commented on Oct 16, 2024

    @theJasonHelmick
    Collaborator

    The Interactive WG has reviewed this issue. This is not a splatting issue. This can be reproduced with a simpler example:

    $a = '*'
    /bin/echo $a

    And Bash has the same behavior in this case:

    # or a='*'
    # or a="*"
    a=*
    /bin/echo $a

    The WG would be interested in seeing more real-world examples of this problem before considering solutions.

  7. added
    WG-Reviewed
    and removed
    WG-NeedsReview
    Needs-Triage
    on Oct 16, 2024
  8. jborean93 commented on Oct 16, 2024

    @jborean93
    Collaborator

    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.

  9. jborean93 commented on Oct 16, 2024

    @jborean93
    Collaborator

    A more specific example, you can pass through a * literal when using it as a positional argument.

    ~/print_argv '*'
    [1] *
    
    ~/print_argv "*"
    [1] *
    
    $w = '*'
    ~/print_argv "$w"
    [1] *

    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 char

    $w = '*'
    $a = @('*')
    
    ~/print_argv @('*')
    ~/print_argv @("*")
    ~/print_argv @("$w")
    ~/print_argv $a
    ~/print_argv @a
  10. mklement0 commented on Oct 17, 2024

    @mklement0
    ContributorAuthor

    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:

    # From Bash:
    # OK: enclosing the array in "..." passes the elements without shell expansions, as individual arguments.
    a=('foo' '*'); printf '%s\n' "${a[@]}"

    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" yields foo * 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-ChildItem calls 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 in find . -iname '*.txt'; trying to implement this as $findArgs = '.', '-iname', '*.txt'; find @findArgs does not work properly, because the up-front expansion of *.txt cannot be prevented)

  11. added
    Resolution-No Activity
    on Apr 16, 2025
  12. Shayan-To commented on Apr 22, 2025

    @Shayan-To

    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.

  13. removed
    Resolution-No Activity
    on Apr 22, 2025
  14. kzrnm commented on Oct 15, 2025

    @kzrnm

    I’m facing exactly this issue in the git-completion-pwsh project I’m developing.
    There’s no workaround when using wildcards as arguments.

    $args = "for-each-ref", "--format=%(refname)", "*", "*/**"
    git @args

    https://github.com/kzrnm/git-completion-pwsh/blob/18b5d008bb6e43e85cb74c79084e165033c4928e/src/Common/GitCommands.ps1#L30

    @args is equivalent to $@ in bash, but the lack of syntax equivalent to "$@" is problematic, in my opinion.

  15. added a commit that references this issue on Oct 16, 2025
  16. kzrnm commented on Oct 16, 2025

    @kzrnm

    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 OSTYPE is set to linux-gnu.

    Note

    Passing multiple --% arguments is ignored altogether, which I believe is a bug. #26198

    Test script

    function Invoke-Printf {
        $inner = $args
        Write-Host -ForegroundColor Yellow "$args"
        Trace-Command -PSHost -Name ParameterBinding -Expression { printf '%s\n' @inner }
    
        Write-Host -ForegroundColor Cyan "$args with --%"
        Trace-Command -PSHost -Name ParameterBinding -Expression { printf '--%' '%s\n' @inner }
    }
    
    $p = 'foo', '*'; $a = 'foo', '--%', '*'
    Invoke-Printf @p
    Invoke-Printf @a

    Result

    PS> function Invoke-Printf {
        $inner = $args
        Write-Host -ForegroundColor Yellow "$args"
        Trace-Command -PSHost -Name ParameterBinding -Expression { printf '%s\n' @inner }
    
        Write-Host -ForegroundColor Cyan "$args with --%"
        Trace-Command -PSHost -Name ParameterBinding -Expression { printf '--%' '%s\n' @inner }
    }
    PS> $p = 'foo', '*'; $a = 'foo', '--%', '*'
    PS> Invoke-Printf @p
    foo *
    DEBUG: 2025-10-16 20:36:06.7626 ParameterBinding Information: 0 : BIND NAMED native application line args [/usr/bin/printf]
    DEBUG: 2025-10-16 20:36:06.7633 ParameterBinding Information: 0 :     BIND cmd line arg [%s\n] to position [0]
    DEBUG: 2025-10-16 20:36:06.7634 ParameterBinding Information: 0 :     BIND cmd line arg [foo] to position [1]
    DEBUG: 2025-10-16 20:36:06.7635 ParameterBinding Information: 0 :     BIND cmd line arg [git-completion] to position [2]
    DEBUG: 2025-10-16 20:36:06.7636 ParameterBinding Information: 0 :     BIND cmd line arg [local] to position [3]
    DEBUG: 2025-10-16 20:36:06.7637 ParameterBinding Information: 0 :     BIND cmd line arg [src] to position [4]
    DEBUG: 2025-10-16 20:36:06.7637 ParameterBinding Information: 0 :     BIND cmd line arg [tests] to position [5]
    DEBUG: 2025-10-16 20:36:06.7637 ParameterBinding Information: 0 :     BIND cmd line arg [testtools] to position [6]
    DEBUG: 2025-10-16 20:36:06.7637 ParameterBinding Information: 0 :     BIND cmd line arg [tools] to position [7]
    DEBUG: 2025-10-16 20:36:06.7638 ParameterBinding Information: 0 :     BIND cmd line arg [3rd-party-license.txt] to position [8]
    DEBUG: 2025-10-16 20:36:06.7638 ParameterBinding Information: 0 :     BIND cmd line arg [CHANGELOG.md] to position [9]
    DEBUG: 2025-10-16 20:36:06.7638 ParameterBinding Information: 0 :     BIND cmd line arg [LICENSE] to position [10]
    DEBUG: 2025-10-16 20:36:06.7638 ParameterBinding Information: 0 :     BIND cmd line arg [README.md] to position [11]
    DEBUG: 2025-10-16 20:36:06.7672 ParameterBinding Information: 0 : CALLING BeginProcessing
    foo
    git-completion
    local
    src
    tests
    testtools
    tools
    3rd-party-license.txt
    CHANGELOG.md
    LICENSE
    README.md
    foo * with --%
    DEBUG: 2025-10-16 20:36:06.7765 ParameterBinding Information: 0 : BIND NAMED native application line args [/usr/bin/printf]
    DEBUG: 2025-10-16 20:36:06.7766 ParameterBinding Information: 0 :     BIND cmd line arg [%s\n] to position [0]
    DEBUG: 2025-10-16 20:36:06.7766 ParameterBinding Information: 0 :     BIND cmd line arg [foo] to position [1]
    DEBUG: 2025-10-16 20:36:06.7766 ParameterBinding Information: 0 :     BIND cmd line arg [*] to position [2]
    DEBUG: 2025-10-16 20:36:06.7777 ParameterBinding Information: 0 : CALLING BeginProcessing
    foo
    *
    PS> Invoke-Printf @a
    foo --% *
    DEBUG: 2025-10-16 20:36:08.2572 ParameterBinding Information: 0 : BIND NAMED native application line args [/usr/bin/printf]
    DEBUG: 2025-10-16 20:36:08.2572 ParameterBinding Information: 0 :     BIND cmd line arg [%s\n] to position [0]
    DEBUG: 2025-10-16 20:36:08.2572 ParameterBinding Information: 0 :     BIND cmd line arg [foo] to position [1]
    DEBUG: 2025-10-16 20:36:08.2573 ParameterBinding Information: 0 :     BIND cmd line arg [*] to position [2]
    DEBUG: 2025-10-16 20:36:08.2584 ParameterBinding Information: 0 : CALLING BeginProcessing
    foo
    *
    foo --% * with --%
    DEBUG: 2025-10-16 20:36:08.2613 ParameterBinding Information: 0 : BIND NAMED native application line args [/usr/bin/printf]
    DEBUG: 2025-10-16 20:36:08.2614 ParameterBinding Information: 0 :     BIND cmd line arg [%s\n] to position [0]
    DEBUG: 2025-10-16 20:36:08.2614 ParameterBinding Information: 0 :     BIND cmd line arg [foo] to position [1]
    DEBUG: 2025-10-16 20:36:08.2614 ParameterBinding Information: 0 :     BIND cmd line arg [*] to position [2]
    DEBUG: 2025-10-16 20:36:08.2623 ParameterBinding Information: 0 : CALLING BeginProcessing
    foo
    *
    PS>
    
  17. added
    Resolution-No Activity
    on Apr 14, 2026
  18. removed
    Resolution-No Activity
    on Apr 15, 2026
  19. added
    Needs-Triage
    WG-NeedsReview
    on Apr 22, 2026
  20. SeeminglyScience commented on Apr 22, 2026

    @SeeminglyScience
    Contributor

    Going to reopen to the interactive WG again, I think there might be more that needs to be discussed here

  21. theJasonHelmick commented on Apr 29, 2026

    @theJasonHelmick
    Collaborator

    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.

  22. added
    KeepOpen
    and removed
    WG-Reviewed
    on Apr 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    KeepOpenNeeds-TriageWG-Interactive-ConsoleWG-NeedsReview

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions