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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



DeepSeek is a powerful intelligent search and analysis tool that provides two access methods: web version and official website. The web version is convenient and efficient, and can be used without installation; the official website provides comprehensive product information, download resources and support services. Whether individuals or corporate users, they can easily obtain and analyze massive data through DeepSeek to improve work efficiency, assist decision-making and promote innovation.

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

BITGet is a cryptocurrency exchange that provides a variety of trading services including spot trading, contract trading and derivatives. Founded in 2018, the exchange is headquartered in Singapore and is committed to providing users with a safe and reliable trading platform. BITGet offers a variety of trading pairs, including BTC/USDT, ETH/USDT and XRP/USDT. Additionally, the exchange has a reputation for security and liquidity and offers a variety of features such as premium order types, leveraged trading and 24/7 customer support.

Gate.io is a popular cryptocurrency exchange that users can use by downloading its installation package and installing it on their devices. The steps to obtain the installation package are as follows: Visit the official website of Gate.io, click "Download", select the corresponding operating system (Windows, Mac or Linux), and download the installation package to your computer. It is recommended to temporarily disable antivirus software or firewall during installation to ensure smooth installation. After completion, the user needs to create a Gate.io account to start using it.

Ouyi OKX, the world's leading digital asset exchange, has now launched an official installation package to provide a safe and convenient trading experience. The OKX installation package of Ouyi does not need to be accessed through a browser. It can directly install independent applications on the device, creating a stable and efficient trading platform for users. The installation process is simple and easy to understand. Users only need to download the latest version of the installation package and follow the prompts to complete the installation step by step.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Ouyi, also known as OKX, is a world-leading cryptocurrency trading platform. The article provides a download portal for Ouyi's official installation package, which facilitates users to install Ouyi client on different devices. This installation package supports Windows, Mac, Android and iOS systems. Users can choose the corresponding version to download according to their device type. After the installation is completed, users can register or log in to the Ouyi account, start trading cryptocurrencies and enjoy other services provided by the platform.
