5 ms·
Came here to mention []. For those who haven't looked it up, in addition to * and ? as wildcard characters, PowerShell supports "[xyz]" and "[a-z]", meaning "x
by 13of40 3y ago
Came here to mention []. For those who haven't looked it up, in addition to * and ? as wildcard characters, PowerShell supports "[xyz]" and "[a-z]", meaning "x y or z" and "anything from a to z", so "[a-z]alls" would match calls, falls, walls, etc.
I knew the guy who tested the feature in PowerShell and he was deeply frustrated over the fact that there was no way to escape some sequences. For example, you can loosely match "[abc]" with something like "?abc?" or "[[]abc?" because the [[] indicates exactly one opening square bracket but there's no way (as far as I know) to say "this section ends with a square bracket".
The PM really wanted that feature, though, even though you could probably count on one hand how many times it's been used in real life.
Edit: That guy down there corrected my example.
- justeleblanc 3y agoUh... You can escape these sequences. But the escape character isn't the backslash for obvious reasons (it's the path separator). It's the backtick. > ni '[abc]' Directory: C:\Users\<user> Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 21/04/2023 12:23 0 [abc] > gi '[abc]' # no output - would have matched files named a, b, or c > gi '`[abc`]' Directory: C:\Users\<user> Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 21/04/2023 12:23 0 [abc] > gi -LiteralPath '[abc]' Directory: C:\Users\<user> Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 21/04/2023 12:23 0 [abc] > gi -lp '[abc]' Directory: C:\Users\<user> Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 21/04/2023 12:23 0 [abc] As far as I know that's been in powershell forever.
- 13of40 3y agoAh, OK. This works because you're using single quotes on your strings, meaning the escape sequence `[ is preserved until it reaches the globbing layer. This will not work, for example... gi "`[abc`]" ...because the `[ will be processed before the string gets sent to globbing, meaning the back tick will be removed. This would work with double quotes: gi "``[abc``]" ...because at the command line level this will evaluate to `[abc`] and the globbing will know that the square brackets are literal. So I will concede and downgrade my complaint from "impossible" to merely "overly complicated for the layman".
- justeleblanc 3y agoAnyone used to scripting languages knows there's a difference between single quotes and double quotes for escaping.
- LoganDark 3y ago> Anyone used to scripting languages knows there's a difference between single quotes and double quotes for escaping. Not necessarily. There are all sorts of scripting languages where single and double quotes are identical. It is, however, super common for there to be escaping differences in shell scripting languages.