`ls` breaks down when path name contains hyphen enclosed in escaped brackets
See original GitHub issuePrerequisites
- 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
I have a file .\o\abc [defg-0 hijk -lmn].pdf
. Then I try all commands below to ls
it. The first char y/x represents that the corresponding command works or not, i.e. prints the file or throw out error which is in ‘Error details’ part.
y ls '*`[defg*0 hijk -lmn`].pdf' -Recurse
y ls '*defg-0 hijk -lmn*.pdf' -Recurse
x ls '*`[defg-0 hijk -lmn`].pdf' -Recurse
Expected behavior
All three commands above work fine, i.e. print the file specified.
Actual behavior
Just as the first char y/x in each command line shows. It seems that hyphen is recognized as a special char although brackets are escaped.
Error details
Get-ChildItem: Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: *\[defg-0 hijk -lmn\].pdf
### Environment data
```powershell
Name Value
---- -----
PSVersion 7.2.6
PSEdition Core
GitCommitId 7.2.6
OS Microsoft Windows 10.0.19043
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_
Issue Analytics
- State:
- Created a year ago
- Comments:12 (4 by maintainers)
Top Results From Across the Web
Why does my shell script choke on whitespace or other ...
There's no direct way to do that. Something like code="$code $filename" breaks if the file name contains any shell special character (spaces, $ ......
Read more >How to handle a closing parenthesis in path names in a for ...
The error message breaks off the path at the first space, which implies that the path was not quoted or escaped.
Read more >How to Create Files with Special Characters in Linux
In this article, we will see how to create, copy, move, and delete filenames that start with spaces and special characters (say #,...
Read more >command line - Problem with spaces in file names
Your first answer fails on filenames that contain backslash, or leading or trailing spaces. Your second answer fails totally unless you add the ......
Read more >Special Characters and Quoting - Learning the bash Shell ...
A few UNIX commands take arguments that often include wildcard characters, which need to be escaped so the shell doesn't process them first....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Amen to that.
If backward compatibility weren’t in the picture, I would eliminate the implicit recursion based on the leaf component of the
-Path
component altogether - it is simply too treacherous and confusing.That is, the only way to search for a name or pattern in all directories of the subtree would then be to use either
-Filter
or-Include
, and both-Path
and-LiteralPath
would consistently only specify the starting point for-Recurse
.Therefore, @iSazonov, backward compatibility also gets in the way of your suggestion, because you can (unfortunately) currently do something like
Get-ChildItem $Home/*.txt -Recurse
too look for*.txt
files in the entire subtree, which would break if$Home/*.txt
were to bind to-Include
.We have
filter
which passes the characters to the Windows API which treats*
and?
but not[ ]
as wildcards (not sure what happens on linux but it may treat them differently)We have
literalpath
which should treat nothing as wildcards even though on windows * and ? are forbidden by the filesystem, on linux that would be how you request a file with those characters.And
path
which treats all 3 as wildcards, unless they are backquoted (the backquote is a requirement of the wildcard class, not PowerShell syntax). However backquoted characters are not always processed correctly.Since
-literalpath
does not work with -recurse ,-filter
will work for this case, but the -path won’t because of the backquote problem.