Home >System Tutorial >LINUX >How to hide your Linux command line history
If you are a Linux command line user, sometimes you may not want certain commands to be recorded in your command line history. There could be many reasons, for example, you hold a certain position in a company and you have certain privileges that you don't want others to abuse. Or maybe there are some particularly important commands that you don't want to execute by mistake while browsing the history list.
However, is there a way to control which commands go into the history list and which don't? Or in other words, can we enable incognito mode like a browser in a Linux terminal? The answer is yes, and depending on the specific goals you want, there are many ways to achieve it. In this article, we’ll discuss some proven methods.
Note: All commands appearing in this article have been tested under Ubuntu.
The first two methods have been described in a previous article. If you already know this, you can skip this part. However, if you don't understand it, it is recommended to read it carefully.
Yes, you read that right. By inserting a space before the command, the command will be ignored by the shell, which means it will not appear in the history. But this method has a prerequisite. It will only work if your environment variable HISTCONTROL is set to "ignorespace" or "ignoreboth". In most cases, this is the default value.
So, like the following command (here [space] means entering a space):
[space]echo "this is a top secret"
If you have previously executed the following command to set environment variables, the above command will not appear in the history.
export HISTCONTROL = ignorespace
The screenshot below is an example of this approach.
The fourth "echo" command is not recorded in the history because there is a space in front of it.
If you want to disable all history for a session, you can simply clear the value of the environment variable HISTSIZE before starting command line work. Execute the following command to clear its value:
export HISTSIZE=0
HISTSIZE indicates the number of commands (number of lines) that can be saved in the history list of a bash session. By default it is set to a non-zero value, for example on my computer it is 1000.
So the above mentioned command sets its value to 0, with the result that nothing will be stored in the history until you close the terminal. Remember that you also cannot see previously executed commands by pressing the up arrow key or running the history command.
This can be seen as another implementation of the solution proposed in the previous part. The only difference is to execute this command after you have finished all your work. The following is the command just mentioned:
history -cw
As mentioned just now, this has the same effect as the HISTSIZE method.
While the previously described methods (2 and 3) will serve the purpose, they can clear the entire history, and in many cases some may not be what we expect. Sometimes you may want to save the history until you started working on the command line. For such needs, you start executing the following commands before working:
[space]set +o history
Note: [space] means space. And because of the spaces, the command itself is not logged either.
The above command will temporarily disable the history function, which means that all operations you perform after this command will not be recorded in the history, but everything before this command will be recorded in the history list as is.
To re-enable the history function, execute the following command:
[Space]set -o history
It will restore the environment to its original state, that is, you have completed your work, and the commands after executing the above command will appear in the history.
现在假设历史记录中已经包含了一些你不希望记录的命令。这种情况下我们怎么办?很简单。直接动手删除它们。通过下面的命令来删除:
history | grep "part of command you want to remove"
上面的命令会输出历史记录中匹配的命令,每一条前面会有个数字。
一旦你找到你想删除的命令,执行下面的命令,从历史记录中删除那个指定的项:
history -d [num]
下面是这个例子的截图。
第二个 ‘echo’命令被成功的删除了。
(如果你不希望上述命令本身也被记录进历史中,你可以在上述命令前加个空格)
同样的,你可以使用向上的箭头一直往回翻看历史记录。当你发现你感兴趣的命令出现在终端上时,按下 “Ctrl + U”清除整行,也会从历史记录中删除它。
有多种不同的方法可以操作 Linux 命令行历史来满足你的需求。然而请记住,从历史中隐藏或者删除命令通常不是一个好习惯,尽管本质上这并没有错。但是你必须知道你在做什么,以及可能产生的后果。
The above is the detailed content of How to hide your Linux command line history. For more information, please follow other related articles on the PHP Chinese website!