If you are a Linux command-line user, sometimes you may not want certain commands to be recorded in your command-line history. There may be many reasons, for example, you have a position in a company where you have some privileges that you do not want to be abused by others. Or some particularly important commands that you do not want to be executed incorrectly when you browse through the history list.
However, is there a way to control which commands go to the History list and which do not? Or in other words, can we open a browser-like pattern in the Linux terminal? The answer is yes, and there are many ways to achieve it, depending on the target you want. In this article, we will discuss some effective methods.
Note: All commands appearing in this article have been tested under Ubuntu.
Different possible methods
The first two methods have been described in the previous article [1]. If you already know, this part can be skipped. However, if you do not understand, it is recommended to read carefully.
1. Insert a space before the command
Yes, it's not wrong. Inserting a space before the command, which is ignored by the shell, means that it does not appear in the history. However, this approach has a premise and only works if your environment variable HISTCONTROL
is set to "Ignorespace" or "Ignoreboth". In most cases, this is the default value.
So, like the following command (LCTT: This [space]
means entering a space):
[space]echo "this is a top secret"
The above command will not appear in the history if you have previously executed a command that sets environment variables as follows.
export HISTCONTROL = ignorespace
The following is an example of this approach.
The fourth "echo" command is not recorded in history because there are spaces in front of it.
2. Disable all history for the current session
If you want to disable all history for a session, you can simply clear HISTSIZE
the value of the environment variable before you start the command line. Execute the following command to clear its value:
export HISTSIZE=0
HISTSIZE
Represents the number of commands (lines) that can be saved for a bash session in its history list. By default, it sets a value other than 0, for example, on my computer, which has a value of 1000.
So the command mentioned above sets its value to 0, and the result is that nothing will be stored in the history until you close the terminal. Remember also that you cannot see the previously executed commands by pressing the UP ARROW keys or running the history command.
3. Clear the whole history after the work is over
This can be seen as another implementation of the scheme mentioned in the previous section. The only difference is that you execute the command after you have done all the work. Here are the commands you just said:
history -cw
As mentioned earlier, this HISTSIZE
has the same effect as the method.
4. Turn off history only for your work
Although the methods described earlier (2 and 3) can achieve the purpose, they can erase the entire history, and in many cases, some may not be what we expect. Sometimes you may want to save the history until you start the command line work. For such a requirement, you begin to execute the following command before you work:
[space]set +o history
Note: [space]
represents a space. And because of the space, the command itself is not recorded.
The above command temporarily disables the history feature, which means that everything you do after this command is not recorded in history, but everything before the command is recorded in the History list as is.
To turn the history feature back on, execute the following command:
[Space]set -o history
It restores the environment, that is, you have done your work, and commands that follow the above command will appear in history.
5. Delete the specified command from the history record
Now assume that the history contains some commands that you do not want to record. What do we do in this situation? Very simple. Remove them directly from the manual. Delete by using the following command:
history | grep "part of command you want to remove"
The command above will output a matching command in the history, and there will be a number in front of each one.
Once you have found the command you want to delete, execute the following command to remove the specified item from the history record:
history -d [num]
Here is an example of this.
The second ' echo ' command was successfully deleted.
(LCTT: If you don't want the above command to be recorded in history, you can add a space before the above command)
Similarly, you can use the UP arrow to go back and look at the history. When you find that the command you are interested in appears on the terminal, pressing " Ctrl + U
" clears the entire line and removes it from the history.
Summarize
There are a number of different ways to manipulate the Linux command-line history to suit your needs. Keep in mind, however, that hiding or deleting commands from history is often not a good habit, although it is not wrong in nature. But you have to know what you are doing and the possible consequences.
Read the original
How to hide the command line history of your Linux