variables - 变量扩展和转义字符

标签 variables powershell

在PowerShell中,您可以在字符串中扩展变量,如下所示:

$myvar = "hello"
$myvar1 = "$myvar`world" #without the `, powershell would look for a variable called $myvarworld
Write-Host $myvar1 #prints helloworld

我遇到的问题是转义字符,例如n r等,如下所示:
$myvar3 = "$myvar`albert"
Write-Host $myvar3 #prints hellolbert as `a is an alert 

还以下不起作用:
$myvar2 = "$myvar`frank" #doesnt work
Write-Host $myvar2 #prints hellorank.

问题:
使用自动变量扩展功能时,如何组合字符串而不必担心转义字符?
还是只需要这样:
$myvar = "hello"
$myvar1 = "$myvar"+"world" #using +
Write-Host $myvar1
$myvar2 = "$myvar"+"frank" #using +

最佳答案

这种方式尚未提及:

"$($myvar)frank"

和这个:
"${myvar}frank"

关于variables - 变量扩展和转义字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6586364/

相关文章:

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

r - 如何评估 R 中带有变量的表达式?

Powershell读取文本文件等到找到然后继续

azure - 使用 Powershell 脚本生成 Azure Databricks token

c# - 使用自己的 PowerShell CmdLets 将管理命令发送到我的 C# Windows 服务

powershell - Chef PowerShell 日志记录/写入主机

C语言,改变char变量

variables - 在 .vbs 的文件路径中使用变量

PHP/MYSQL : What's Wrong With My Query/PHP variable in query

powershell - 比较字符串和数组时,PowerShell Substiture -eq是否包含-eq