为什么$(写入主机)的子表达式输出到字符串的开头?

时间:2016-10-01 04:07:35

标签: powershell

在$()子表达式中使用Write-Host始终输出到字符串的开头,而不管其位置如何。

例如:

"This is $(Write-Host 'now at the beginning' -NoNewline)"

输出:

now at the beginningThis is

其他cmdlet在此处按预期工作(例如"Today is $((Get-Date).DayOfWeek)"Today is Friday)。

写主机有何不同?

1 个答案:

答案 0 :(得分:3)

它没有输出到字符串"的开头,它向主机写入两次,向后'顺序。

  1. "abc $()"是一个包含子表达式的字符串文字。
  2. 计算字符串是什么,意味着评估$()
  3. 评估$()写入屏幕now at the beginning并且不返回任何内容
  4. 现在字符串文字为"abc"
  5. 现在"abc"已写入屏幕now at the beginningabc
  6. 这不符合你的建议:

    1. "abc $(write-host 'hi')"
    2. "hiabc"
    3. 写入屏幕。
相关问题