Strange behavior in PS7 with replacing specific bytes in While-clause
See original GitHub issueFor no other reason than to challenge myself, I tried to make one of my scripts to use as less bytes as possible.
I need to find all double closing brackets and separate them:
while($l.Contains("))")){$l=$l.Replace("))",")`t)")}
# )) -> )`t)
# ))) -> )`t)`t)
To describe this here I use a TAB to have a visible character. In my code I pasted a [char]7
(that will never be in the input text).
This works in 5.1, 6.2 and 7.1
To make my code shorter I used this:
while($l-ne($l=$l.Replace("..",".`t"))){}
This also works in 5, 6 and 7 if you use a TAB (pasted, not `t).
With [char]7
it’s OK in Powershell 5 and 6, but fails in 7.
Actual behavior in PS7
$code = '$l = "#####" ; while($l-ne($l=$l.Replace("##","#Q#"))){} ; $l.Split("Q") -join "-"'
1..31 | %{"{0:00} - {1}" -f $_, (Invoke-Expression $code.Replace("Q",[char]$_))}
1 - #-##-##
2 - #-##-##
3 - #-##-##
4 - #-##-##
5 - #-##-##
6 - #-##-##
7 - #-##-##
8 - #-##-##
9 - #-#-#-#-#
10 - #-#-#-#-#
11 - #-#-#-#-#
12 - #-#-#-#-#
13 - #-#-#-#-#
14 - #-##-##
15 - #-##-##
16 - #-##-##
17 - #-##-##
18 - #-##-##
19 - #-##-##
20 - #-##-##
21 - #-##-##
22 - #-##-##
23 - #-##-##
24 - #-##-##
25 - #-##-##
26 - #-##-##
27 - #-##-##
28 - #-##-##
29 - #-##-##
30 - #-##-##
31 - #-##-##
The results of bytes 9 to 13 are as expected. In PS5 or PS6 all bytes result in the correct output.
Why does this behave different for specific values?
Environment data
Name Value
---- -----
PSVersion 7.1.2
PSEdition Core
GitCommitId 7.1.2
OS Microsoft Windows 10.0.18363
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Bash shell scripting: replace n bytes in a specific position ...
I want to replace these characters with spaces in each line of the file. In java I can get the desired result by...
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 FreeTop 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
Top GitHub Comments
I agree, @vexx32 - a cursory web search didn’t provide any clues.
Searching the issues at https://github.com/dotnet/runtime/issues I did find this, which may explain the change (I haven’t dug deeper):
https://github.com/dotnet/runtime/issues/43736#issuecomment-714779040
The document update PR is out: https://github.com/MicrosoftDocs/PowerShell-Docs/pull/8672 Thanks @sdwheeler!