繁体   English   中英

在PowerShell中转义单引号

[英]Escape single quotes in PowerShell

我正在尝试构建类似于以下内容的SharePoint 2010 listdata.svc查询:

http://server/FooList/_vti_bin/listdata.svc/FooList?$select=Title,Id,OwnerId,Status&$filter=(OwnerId eq 1234 and Status eq 'Ready')

单引号无效:

$url = $url + '&$filter=(OwnerId eq 1234 and Status eq 'Ready')'

也不用反斜杠转义:

$url = $url + '&$filter=(OwnerId eq 1234 and Status eq `'Ready`')'

如果我使用双引号,则将$filter替换为零长度的字符串(它不是脚本中的变量):

$url = $url + "&$filter=(OwnerId eq 1234 and Status eq 'Ready')"

转义单引号的正确方法是什么?

使用单引号字符串文字时,您需要将双引号引起来:

'&$filter=(OwnerId eq 1234 and Status eq ''Ready'')'
                                         ^^     ^^

演示:

PS > '&$filter=(OwnerId eq 1234 and Status eq ''Ready'')'
&$filter=(OwnerId eq 1234 and Status eq 'Ready')
PS >

使用反引号仅适用于带双引号的字符串文字(因为它们处理转义序列),但是随后还需要转义所有其他特殊字符(例如,变量名中的$ ):

PS > "&`$filter=(OwnerId eq 1234 and Status eq `"Ready`")"
&$filter=(OwnerId eq 1234 and Status eq "Ready")
PS >

但是在单引号字符串文字中, `被视为文字反引号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM