在 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 ScriptBlock returns
$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/