1

According to Powershell "about quoting rules":

A here-string is a single-quoted or double-quoted string in which quotation marks are interpreted literally.

However the following here-string results in a TerminatorExpectedAtEndOfString exception in Powershell version 5.0 (build 10586, revision 117). In a Powershell version 2.0 it works as expected:

$herestr=@"
'"'
"@
Write-Host $herestr

If I quote the last single quote with a backtick (`) the here-string works as expected in both version 2.0 and 5.0.

$herestr=@"
'"`'
"@
Write-Host $herestr

Why do I need to escape the last single quote in a here-string?

  • 1
    IMHO, it is a bug in PowerShell parser. When in statement parse mode, it first try to interpret first thing as BareWord string literal. And if it failed, then it produce error. You can workaround this by putting extra space: $herestr =@"... or $herestr= @"...; or by enclosing in parenthesis: ($herestr=@"..."@). – PetSerAl Nov 22 '16 at 17:11
  • @PetSerAl: thanks for the feedback. Do you know if this is known bug or should I report it? – Bram Nov 24 '16 at 9:54
  • I think your bug have the same root cause as this one. – PetSerAl Nov 24 '16 at 10:34
  • @PetSerAl: thanks: I created an issue on github as well since it's not clear if the uservoice forum is still being used: github.com/PowerShell/PowerShell/issues/2780 . If you post the workarounds you listed as an answer I'll accept it. In the mean time I found that the Posh style guide recommends to use spaces before and after the equal signs so that seems like the right way to work around this bug. – Bram Nov 25 '16 at 11:37
1

I believe it is a bug in PowerShell parser. It looks like when it in statement parse mode, it first try to interpret first thing as BareWord string literal. And if it failed, then it produce error. You can workaround this by putting extra space before or after equal sign:

$herestr =@"
'"'
"@

or

$herestr= @"
'"'
"@

Or by using parenthesis:

[void]($herestr=@"
'"'
"@)

Note, as parenthesis is not assignment or increment/decrement, them will write result of expression into pipeline, unlike bare assignment expression, so you need to explicitly ignore it with [void] or any other method.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.