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?

4
  • 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=@"..."@).
    – user364455
    Commented Nov 22, 2016 at 17:11
  • @PetSerAl: thanks for the feedback. Do you know if this is known bug or should I report it?
    – Bram
    Commented Nov 24, 2016 at 9:54
  • I think your bug have the same root cause as this one.
    – user364455
    Commented Nov 24, 2016 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
    Commented Nov 25, 2016 at 11:37

1 Answer 1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .