PowerShell逃脫字元問題 - Windows

Table of Contents

作業系統: Windows 10

Service Pack: 20H2

發生問題頻率: 100%

是否有做Windows Update: 有

問題內容:

據我所知Powershell中要表示
字元` = '``' = "````"
字元' = '''' = "`'"
字元[ = '`[' = "``["
字元] = '`]' = "``]"

依此類推...

# D:\`[test`]'`
test-path 'D:\```[test```]''``'
test-path "D:\``````[test``````]`'````"

# E:\`[test`]'``
test-path 'E:\```[test```]''````'
test-path "E:\``````[test``````]`'````````"

以上皆如預期回傳 True

# D:\`[test`]'`
test-path 'D:\```[test```]''`'
test-path "D:\``````[test``````]`'``"

# E:\`[test`]'``
test-path 'E:\```[test```]''```'
test-path "E:\``````[test``````]`'``````"

上面這些怎麼也全都回傳 True?
單引號內右側少了1個`
雙引號內右側少了2個`

想了一下...
看起來'`'或"``"右側沒有跟其他特殊字元相鄰時等同'``'或"````"
所以test-path中 '`' = '``' = "````" = "``````" = "````````"
怎麼會有這麼奇怪的行為? 還是我哪裡誤會了?

--

All Comments

Andrew avatarAndrew2020-10-28
遇到引號建議使用 Test-Path -LiteralPath