PowerShell: Double Quoted String, String Expansion
Double Quoted String, String Expansion
Character sequence between "double quote"
is expandable string.
(aka interpolated string)
Variable or expression inside are replaced by their values.
$n = 4 $x = "I have $n cats" Write-Host $x # I have 4 cats
Expression is also expanded.
$x = "I have $(2+1) cats" Write-Host $x # I have 3 cats
To include a newline, use literal newline or use `n
.
$x = "A`nB" $y = "A B" $x -eq $y
Variable Inside Double Quote
- It should have the form
${name}
. - The curly brackets can be omitted if there is no space after the name, nor some other non-letter character.
$x = 4 "${x}cats" -eq "4cats"
To make the dollar sign string sequence literal, add a GRAVE ACCENT ` before it.
$x = 4 "`${x}cats" -eq '${x}cats'
Expression Inside Double Quote
It should have the form
$(expr)
.
"$(3+4)cats" -eq '7cats'
Escape Character
To include a double quote inside a double quoted string, precede it with GRAVE ACCENT `
$x = "He said: `"yes`""
Or precede it with double quote.
$x = "He said: ""yes"""
〔see Escape Characters〕
PowerShell String
- PowerShell: Quote String
- PowerShell: Double Quoted String, String Expansion
- PowerShell: Here-String
- PowerShell: Escape Characters
- PowerShell: String Operators
- PowerShell: Join String
- PowerShell: Split String
- PowerShell: Format String
- PowerShell: String Methods
- PowerShell: String Wildcards
- PowerShell: Regular Expression Operators
- PowerShell: Regular Expression Syntax