Search code examples
powershellexpressiondouble-quotessingle-quotes

powershell @' '@ contains @''@ what should I do


In powershell, if I use @ "" @, then if the string contains $var, the $var will be parsed, so I can only use @ ', but to my surprise @ 'won't work properly either enter image description here

If you just do that, you'll get an error, so that won't work enter image description here

Microsoft has told me that I can use two single quotes, but after I use two single quotes, the output is still two single quotes, the first single quote is not escaped, what should I do? enter image description here

This is my powershell version. I'm using Windows 10 enter image description here

I tried using @" but it won't work because the string contains $var. So, I had to use @', Microsoft's bing ai told me I could use """, but it was deceiving me, powershell doesn't have an expression like three double quotes


Solution

  • You can still use a double quoted here string, @" and "@.

    All you need to do is escape the dollar sign in $var, with a backtick, as follows.

    $a = @"
    
    aaa
    
        `$var = 'I need this'
    
    "@
    
    echo $a