question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`ls` breaks down when path name contains hyphen enclosed in escaped brackets

See original GitHub issue

Prerequisites

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:open
  • Created a year ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mklement0commented, Sep 9, 2022

This is all harder than it needs to be 😦

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.

1reaction
jhoneillcommented, Sep 8, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found