How to hide Linux command line history
- Blog
- 2023.07.27
- 1 views
As a user of the Linux command line, there are times when you may not want certain commands to be recorded in your command line history. There can be many reasons for this, for example if you have a certain position in your company and you have some privileges that you don't want to be abused by other people. Or maybe there are some particularly important commands that you don't want to accidentally run while browsing the history list.
But is there a way to control which commands go into the history list and which don't? In other words, can we enable a browser-like no-trace mode in the Linux terminal? The answer is yes, and there are many ways to accomplish this, depending on your specific goals. In this article, we'll discuss a few proven methods.
1. Insert a space before the command
Yes, that's right. If you put a space in front of a command, the shell will ignore it, i.e. it won't appear in the history. But this method has a caveat: it only works if your HISTCONTROL environment variable is set to "ignorespace" or "ignoreboth". In most cases, this is the default.
So, a command like the following (LCTT note: here [space] means enter a space):
[space]echo "this is a top secret"
If you have previously run the following commands to set environment variables, the above commands 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 it is preceded by a space.
2. Disable all history for the current session
If you want to disable all history for a particular session, you can simply clear the value of the HISTSIZE environment variable before you start working at the command line. To clear the value, run the following command:
export HISTSIZE=0
HISTSIZE is the number of commands (lines) that can be stored in the history list for a bash session. By default it is set to a non-zero value, e.g. on my machine it is 1000.
So the above command sets it to 0, with the result that nothing is saved to the history until you close the terminal. Also remember that you can't see previously executed commands by pressing the up arrow key or running the history command.
3. Clearing the entire history at the end of the work
This can be seen as an alternative implementation of the scheme suggested in the previous section. The only difference is that you run this command after you have done all the work. Here is the command just mentioned:
history -cw
As mentioned earlier, this has the same effect as the HISTSIZE method.
4. Close the history for your work only
While the previously described methods (2 and 3) accomplish the purpose, they erase the entire history, and in many cases, some of it may not be what we expect. Sometimes you may want to save the history between the time you started working at the command line. For such a need, run the following command before you start working:
[space]set +o history
Notes: [space] means space. And the command itself is not logged because of spaces.
The above command temporarily disables the history feature, which means that anything you do after this command will not be logged in the history, but everything before this command will be logged in the history list as is.
To re-enable the history feature, run the following command:
[Space]set -o history
It restores the environment to its original state, i.e. you are done with your work and commands after executing the above commands appear in the history.
5. Delete the specified command from the history record
Now let's say that the history already contains some commands that you don't want to be recorded. What do we do in this case? Simple. Just go ahead and delete them. Delete them with the following command:
history | grep "part of command you want to remove"
The above command lists the matching commands in the history, each preceded by a number.
When you find the command you want to remove, run the following command to remove the specified item from the history:
history -d [num]
Again, you can use the up arrow to go all the way back through the history. When you find the command you're interested in in the terminal, pressing "Ctrl + U" will delete the entire line and also remove it from the history.