变量扩展和转义字符 [英] Variable expansion and escaped characters

查看:37
本文介绍了变量扩展和转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In PowerShell, you can expand variables within strings as shown below:

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

我遇到的问题是转义字符,如 nr 等,如下所示:

The problem I am having is with escaped characters like nr etc, as shown below:

$myvar3 = "$myvar`albert"
Write-Host $myvar3 #prints hellolbert as `a is an alert 

以下也不起作用:

$myvar2 = "$myvar`frank" #doesnt work
Write-Host $myvar2 #prints hellorank.

问题:当我使用自动变量扩展功能时,如何组合字符串而不必担心转义字符?还是我必须这样做:

Question: How do I combine the strings without worrying about escaped characters when I am using the automatic variable expansion featurie? Or do I have to do it only this way:

$myvar = "hello"
$myvar1 = "$myvar"+"world" #using +
Write-Host $myvar1
$myvar2 = "$myvar"+"frank" #using +

推荐答案

这种方式暂未提及:

"$($myvar)frank"

还有这个:

"${myvar}frank"

这篇关于变量扩展和转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆