执行命令而不将其保留在历史记录中

发布于 2024-12-20 21:23:18 字数 1819 浏览 1 评论 0原文

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

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

发布评论

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

评论(13

稍尽春風 2024-12-27 21:23:18

以空格开头的命令将不会包含在历史记录中。

请注意,这确实需要设置环境变量$HISTCONTROL

  • 检查以下命令是否返回ignorespace
    忽略两者

     echo $HISTCONTROL
    
  • 要添加环境变量(如果缺少),可以将以下行添加到 Bash 配置文件中。例如,文件 %HOME/.bashrc

     导出 HISTCONTROL=ignorespace
    

再次采购配置文件后,空格前缀的命令将不会写入<代码>$HISTFILE。

Start your command with a space and it won't be included in the history.

Be aware that this does require the environment variable $HISTCONTROL to be set.

  • Check that the following command returns ignorespace or
    ignoreboth:

     echo $HISTCONTROL
    
  • To add the environment variable if missing, the following line can be added to the Bash profile. E.g., to file %HOME/.bashrc.

     export HISTCONTROL=ignorespace
    

After sourcing the profile again, space-prefixed commands will not be written to $HISTFILE.

木落 2024-12-27 21:23:18

在任何给定的 Bash 会话中,通过键入以下内容将历史文件设置为 /dev/null:

export HISTFILE=/dev/null

请注意,正如注释中所指出的,这不会将该会话中的任何命令写入历史记录!

请不要打扰系统管理员的辛勤工作;)

Doodad 的解决方案 更加优雅。只需取消设置变量:unset HISTFILE(谢谢!)

In any given Bash session, set the history file to /dev/null by typing:

export HISTFILE=/dev/null

Note that, as pointed out in the comments, this will not write any commands in that session to the history!

Just don't mess with your system administrator's hard work, please ;)

Doodad's solution is more elegant. Simply unset the variable: unset HISTFILE (thanks!)

時窥 2024-12-27 21:23:18
echo "discreet";history -d $(history 1)
echo "discreet";history -d $(history 1)
迟月 2024-12-27 21:23:18

John Doe塞德里克·罗耶的回答。但是,这似乎对我有用。

<your_secret_command>; history -d $((HISTCMD-1))

您不应在历史记录中看到该命令的条目。

这是解释...

“history -d”从历史记录中删除提到的条目。

HISTCMD 存储下一个要执行的命令号。因此,(HISTCMD-1) 指的是最后执行的命令。

从 Bash 历史文件中删除特定行

An extension of John Doe's and Cédric ROYER's answer. But, this seems to work for me.

<your_secret_command>; history -d $((HISTCMD-1))

You should not see the entry of the command in your history.

Here's the explanation...

The 'history -d' deletes the mentioned entry from the history.

The HISTCMD stores the command_number of the one to be executed next. So, (HISTCMD-1) refers to the last executed command.

Remove a certain line from Bash history file

入画浅相思 2024-12-27 21:23:18

如果您使用 Z shell (zsh),您可以运行

setopt histignorespace

:设置后,每个以空格开头的命令都将从历史记录中排除。

您可以在 .zshrc 中使用别名打开/关闭

# Toggle ignore-space. Useful when entering passwords.
alias history-ignore-space-on='\
setopt hist_ignore_space;\
echo "Commands starting with space are now EXCLUDED from history."'

alias history-ignore-space-off='\
unsetopt hist_ignore_space;\
echo "Commands starting with space are now ADDED to history."'

If you are using Z shell (zsh) you can run:

setopt histignorespace

After this is set, each command starting with a space will be excluded from history.

You can use aliases in .zshrc to turn this on/off:

# Toggle ignore-space. Useful when entering passwords.
alias history-ignore-space-on='\
setopt hist_ignore_space;\
echo "Commands starting with space are now EXCLUDED from history."'

alias history-ignore-space-off='\
unsetopt hist_ignore_space;\
echo "Commands starting with space are now ADDED to history."'
无法回应 2024-12-27 21:23:18

您可能会考虑使用没有历史记录的 shell,例如

/bin/sh << END
   your commands without history
END

(也许 /bin/dash/bin/sash 可能比 /bin/sh 更合适code>)

或者更好的是,使用 batch 实用程序,例如,

batch << EOB
   your commands
EOB

历史记录将包含 shbatch 这没有多大意义。

You might consider using a shell without history, like perhaps

/bin/sh << END
   your commands without history
END

(perhaps /bin/dash or /bin/sash could be more appropriate than /bin/sh)

Or even better, use the batch utility, e.g.,

batch << EOB
   your commands
EOB

The history would then contain sh or batch which is not very meaningful.

撩心不撩汉 2024-12-27 21:23:18

您可以开始您的会话

export HISTFILE=/dev/null ;history -d $(history 1)

,然后继续您的偷偷摸摸的行为。将 histfile 设置为 /dev/null 将被记录到历史文件中,但此条目将很容易被删除,并且不会显示任何痕迹(至少在历史文件中)。

而且,这是非永久性的。

You can start your session with

export HISTFILE=/dev/null ;history -d $(history 1)

then proceed with your sneaky doings. Setting the histfile to /dev/null will be logged to the history file, yet this entry will be readily deleted and no traces (at least in the history file) will be shown.

Also, this is non-permanent.

彼岸花ソ最美的依靠 2024-12-27 21:23:18

正如Doodad 在评论中提到的unset HISTFILE 可以很好地做到这一点,但如果您还想删除一些历史记录,请执行 echo $HISTFILE 获取历史文件位置(通常是 ~/.bash_history),取消设置 HISTFILE,并编辑 ~/.bash_history (或者任何HISTFILE - 当然它现在未设置所以你无法读取它)。

$ echo $HISTFILE       # E.g. ~/.bash_history
$ unset HISTFILE
$ vi ~/.bash_history   # Or your preferred editor

然后你就编辑了你的历史记录,并且事实上你编辑了它!

As mentioned by Doodad in comments, unset HISTFILE does this nicely, but in case you also want to also delete some history, do echo $HISTFILE to get the history file location (usually ~/.bash_history), unset HISTFILE, and edit ~/.bash_history (or whatever HISTFILE was - of course it's now unset so you can't read it).

$ echo $HISTFILE       # E.g. ~/.bash_history
$ unset HISTFILE
$ vi ~/.bash_history   # Or your preferred editor

Then you've edited your history, and the fact that you edited it!

旧伤慢歌 2024-12-27 21:23:18

您还可以使用以下命令:

echo toto; history -d $(history | sed -n '$s/\s*\([0-9]*\)\s*.*$/\1/p')

我认为这是一个非常可移植的命令。

You can also use the following command:

echo toto; history -d $(history | sed -n '$s/\s*\([0-9]*\)\s*.*$/\1/p')

I think it's a very portable command.

遇见了你 2024-12-27 21:23:18

有多种方法可以实现这一目标。这将历史文件的大小设置为 0:

export HISTFILESIZE=0

这将历史文件设置为 /dev/null,从而有效地禁用它:

export HISTFILE=/dev/null

对于单个命令,您可以在命令前添加空格,但它不会保存在历史文件中。请注意,这要求您将 ignorespace 值包含在 $HISTCONTROL 环境变量中(man bash 并搜索 ignorespace 了解更多详细信息)。

There are several ways you can achieve this. This sets the size of the history file to 0:

export HISTFILESIZE=0

This sets the history file to /dev/null, effectively disabling it:

export HISTFILE=/dev/null

For individual commands, you can prefix the command with a space and it won't be saved in the history file. Note that this requires you have the ignorespace value included in the $HISTCONTROL environment variable (man bash and search for ignorespace for more details).

阪姬 2024-12-27 21:23:18

你只需要运行:
$ set +o History

要查看更多信息,请运行:
$ 人集

You just need to run:
$ set +o history

To see more, run:
$ man set

你的笑 2024-12-27 21:23:18

这个命令可能会派上用场。这样不会记录执行的命令

history -d $((HISTCMD-1)) && <Your Command Here>

This command might come in handy. This will not record the command that is executed

history -d $((HISTCMD-1)) && <Your Command Here>
冰之心 2024-12-27 21:23:18

如果您想删除所有历史记录,包括您删除了所有历史记录,这非常方便!

rm .bash_history;export HISTFILE=/dev/null;exit

This is handy if you want to erase all the history, including the fact that you erased all the history!

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