gpt4 book ai didi

shell - 如何防止在 PowerShell 中保存以空格开头的输入历史记录?

转载 作者:行者123 更新时间:2023-12-02 23:18:45 31 4
gpt4 key购买 nike

在 bash 中(至少在 Ubuntu 中),可以不保存历史记录中以空格开头的命令(HISTCONTROL)。
有没有办法在 Powershell 中获得此功能?

最佳答案

至少从 PowerShell 5.1 开始,您可以使用 Set-PSReadlineOption -AddToHistoryHandler验证是否应使用自定义函数将命令添加到历史记录中。

-AddToHistoryHandler Specifies a ScriptBlock that controls which commands get added to PSReadLine history.

The ScriptBlock receives the command line as input. If the ScriptBlockreturns $True, the command line is added to the history.


为了完整起见,这里有一个代码示例,您可以添加到您的 $PROFILE.CurrentUserAllHosts 中。
function Check-HistoryCommand($command)
{
if ($command -like ' *') {
return $false
}
# Add any other checks you want
return $true
}

Set-PSReadLineOption -AddToHistoryHandler ( Get-Item Function:Check-HistoryCommand ).ScriptBlock

关于shell - 如何防止在 PowerShell 中保存以空格开头的输入历史记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57705570/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com