Home > Software engineering >  Powershell invoke tool with argument containing a wildcard
Powershell invoke tool with argument containing a wildcard

Time:09-07

I'm writing a tool that receives an argument that is a file pattern as follows:

mytool arg1 *

Here is the problem: The character * is being resolved to the contents of the current directory, almost like the ls command. If I escape the wildcard using grave (`) it is also received by the command.

How can I solve that problem?

CodePudding user response:

Use a verbatim (single-quoted) string ('...')[1] to prevent the globbing (pathname extension) that PowerShell performs on Unix-like platforms (only) when calling external programs:

mytool arg1 '*'

I have no explanation for why character-individual escaping (`*) doesn't work (as of PowerShell 7.2.6) - arguably, it should; the problem has been reported in GitHub issue #18038.


On Windows, PowerShell performs no globbing, and *, '*', and `* are all passed as verbatim * to an external program.

As you state, given that you're on Windows, it must be the Python-based tool you're calling that performs globbing.


[1] An expandable (double-quoted) string ("...") string works too, but there's no need for one unless you truly need expansion (string interpolation).

  • Related