powershell中单引号和双引号定义字符串有什么区别

发布于 2024-11-06 04:25:11 字数 166 浏览 0 评论 0原文

一直困扰我的简单问题:在 powershell 中,我可以像这样定义字符串:

$s1 = "Boogety boo"

或者

$s2 = '.net rocks'

解释器有什么区别吗?

Simple questions that's been bugging me: In powershell, I can define strings like so:

$s1 = "Boogety boo"

or

$s2 = '.net rocks'

Is there a difference to the interpreter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘦怹 2024-11-13 04:25:11

双引号允许变量扩展,而单引号则不允许:

PS C:\Users\Administrator> $mycolor="red"
PS C:\Users\Administrator> write-output -inputobject 'My favorite color is $mycolor'
My favorite color is $mycolor

来源:http://www.techotopia。 com/index.php/Windows_PowerShell_1.0_String_Quoting_and_Escape_Sequences

(我知道1.0版但原理还是一样的)

Double quotes allow variable expansion while single quotes do not:

PS C:\Users\Administrator> $mycolor="red"
PS C:\Users\Administrator> write-output -inputobject 'My favorite color is $mycolor'
My favorite color is $mycolor

Source: http://www.techotopia.com/index.php/Windows_PowerShell_1.0_String_Quoting_and_Escape_Sequences

(I know version 1.0 but the principle is still the same)

恰似旧人归 2024-11-13 04:25:11

这并不是试图成为更好的答案。只是另一种说法。

撇号和引号之间的变量扩展与 UNIX shell(sh、ksh、bash)上的相同。使用撇号将按原样获取字符串,而不处理任何转义。

PS C:\Users\lit> $x = "`t"
PS C:\Users\lit> $x

PS C:\Users\lit> Write-Output "now${x}is"
now     is
PS C:\Users\lit> $x = '`t'
PS C:\Users\lit> $x
`t
PS C:\Users\lit> Write-Output "now${x}is"
now`tis
PS C:\Users\lit> $word = "easy"
PS C:\Users\lit> "PowerShell is $word"
PowerShell is easy
PS C:\Users\lit> 'PowerShell is $word'
PowerShell is $word

This is not trying to be a better answer. Just another way to say it.

The variable expansion between apostrophes and quotes are the same as on UNIX shells (sh, ksh, bash). Using apostrophes will take the character string as-is, without processing any escapes.

PS C:\Users\lit> $x = "`t"
PS C:\Users\lit> $x

PS C:\Users\lit> Write-Output "now${x}is"
now     is
PS C:\Users\lit> $x = '`t'
PS C:\Users\lit> $x
`t
PS C:\Users\lit> Write-Output "now${x}is"
now`tis
PS C:\Users\lit> $word = "easy"
PS C:\Users\lit> "PowerShell is $word"
PowerShell is easy
PS C:\Users\lit> 'PowerShell is $word'
PowerShell is $word
唐婉 2024-11-13 04:25:11

这个问题在 about_Quoting_Rules< 中有直接答案/a> PowerShell 文档的文章:

双引号字符串

用双引号括起来的字符串是可扩展字符串。在将字符串传递给命令进行处理之前,前面带有美元符号 ($) 的变量名称将替换为变量的值。

例如:

<前><代码>$i = 5
“$i 的值是 $i。”

该命令的输出是:

5 的值为 5。

单引号字符串

用单引号括起来的字符串是逐字字符串。该字符串将与您键入的内容完全相同地传递给命令。不执行替换。例如:

<前><代码>$i = 5
“$i 的值是 $i。”

该命令的输出是:

$i 的值为 $i。

换句话说,如果您希望字符串保持原样,请使用单引号。如果要插入变量 ($myVariable)、命令执行结果和其他评估 ($($myList -join ', ')) 或特殊值,请使用双引号字符(`r`n`t`a 等)。

This question has a direct answer in the about_Quoting_Rules article of the PowerShell docs:

Double-quoted strings

A string enclosed in double quotation marks is an expandable string. Variable names preceded by a dollar sign ($) are replaced with the variable's value before the string is passed to the command for processing.

For example:

$i = 5
"The value of $i is $i."

The output of this command is:

The value of 5 is 5.

Single-quoted strings

A string enclosed in single-quotation marks is a verbatim string. The string is passed to the command exactly as you type it. No substitution is performed. For example:

$i = 5
'The value of $i is $i.'

The output of this command is:

The value of $i is $i.

In other words, use single quotes if you want your string to remain as written. Use double quotes, if you want to insert variables ($myVariable), results of command executions and other evaluations ($($myList -join ', ')) or special characters (`r, `n, `t, `a, etc.).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文