Atomic Habits

What is the purpost of the &{} syntax ? 본문

카테고리 없음

What is the purpost of the &{} syntax ?

체계성 2022. 7. 7. 09:49

https://stackoverflow.com/questions/47760921/what-is-the-purpose-of-the-syntax-operator

 

What is the purpose of the ${} syntax/operator?

Edit: My additional thoughts on the functionality of the operator- It appears to implement the IContentCmdletProvider interface which applies to the env:, function:, and drive: PSDrives. (which is

stackoverflow.com

 

So ${} has two different purposes. One of them is variable declaration, as it enables you to enclose any special characters in your variable name:

${special var with spaces}

But also allows you to use it in a larger string like:

$Var="middle"
Write-Host "Before${Var}After"

Another use cases, as you are suggesting is that it is able to read paths.

$Var = ${C:\Temp\Filename.txt}

is the same as

$Var = Get-Content -Path 'C:\Temp\Filename.txt'

But also to write to a path:

${C:\Temp\Filename.txt} = 'This writes to the file!'

is the same as

'This writes to the file!' | Set-Content -Path 'C:\Temp\Filename.txt'
Comments