"%" 是什么意思(百分比)在 PowerShell 中做什么? [英] What does "%" (percent) do in PowerShell?

查看:49
本文介绍了"%" 是什么意思(百分比)在 PowerShell 中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎 % 操作在管道之后启动脚本块,尽管 about_Script_Blocks 表示不需要 %.

It seems that the % operation starts script blocks after the pipeline, although about_Script_Blocks indicates the % isn't necessary.

这些都很好.

get-childitem | % { write-host $_.Name }

{ write-host 'hello' }

% { write-host 'hello' }

但是当我们在管道之后添加一个脚本块时,我们需要先有 %.

But when we add a script block after the pipeline, we need to have the % first.

get-childitem | { write-host $_.Name }

推荐答案

在 cmdlet 的上下文中使用时(例如您的示例),它是 ForEach-Object 的别名:

When used in the context of a cmdlet (such as your example), it's an alias for ForEach-Object:

> Get-Alias -Definition ForEach-Object

CommandType     Name                                                Definition
-----------     ----                                                ----------
Alias           %                                                   ForEach-Object
Alias           foreach                                             ForEach-Object

在等式上下文中使用时,它是 模运算符:

When used in the context of an equation, it's the modulus operator:

> 11 % 5

1

作为模运算符,% 也可以用于 赋值运算符 (%=):

and as the modulus operator, % can also be used in an assignment operator (%=):

> $this = 11
> $this %= 5
> $this

1

这篇关于"%" 是什么意思(百分比)在 PowerShell 中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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