Let's suppose we want to run a command that shouldn't be saved in history. For this example, I'll 1
use wget and download a file locally. 2
And I'll download the logo image of Google. 3
This one! Copy image location and in the terminal, I'm writing wget and I'm pasting 4
the contents of the clipboard. 5
The file was saved locally, in the user's home directory. 6
This is the file. 7
Now, the admin or other users that have access to this account can see the command I've run by executing 8
history. 9
What can I do? if I erase the history by running history -c? 10
It will look suspicious. Why would someone erase his entire history ? If you manually edit the history 11
file, the command that opens the file for editing will be saved in the history as well, 12
and it's not good. 13
This will be saved in the history. 14
It's not good. 15
A possible solution is to type a space in front of a command, before running it in the bash shell and 16
the command will run normally but won't appear in your history. 17
Let's do it both on Ubuntu and CentOS. I'm running the WHO command, which will display the users logged 18
into the system. 19
And I'm checking the history. 20
And of course, the command is there! Now I write the white space and the command again. 21
And I'm checking the history. 22
And the command was not saved. 23
who-a was not saved. 24
Let's do the same on CentOS. 25
I'm writing a white space and the command. 26
And I'm checking the history. 27
This time the command is there, even though I've added a whitespace before the command name. 28
So we see that the behavior is not the same on different Linux distributions. 29
Let me tell you in detail how that works. 30
There is a special variable called HISTCONTROL that allows you to ignore commands and not save them in 31
bash history if you set the variable to ignorespace. 32
Let’s see its value on both Ubuntu and CentOS. echo $ and the variable name HISTCONTROL 33
So HISTCONTROL is set to ignore both on Ubuntu. 34
And now on CentOS 35
Don't forget to write a dollar sign before the variable's name. 36
On CentOS the variable is set to ignore dups; let's set it to ignorespace on CentOS. 37
HISTCONTROL written in uppercase letters =ignoredspace 38
ignorespace written in lowercase letters. 39
Now I'm running a command adding a whitespace before its name. 40
Let's check the history. 41
And the command was not recorded. 42
But what will happen if I run the same command multiple times. Will it record each command or ignore 43
duplicates? 44
I'm running the cd command multiple times. 45
Five times. 46
And I'm listing the history ; we see that it has recorded each command, which maybe it’s not what 47
we really want. 48
Maybe you want your history a bit cleaner by ignoring duplicate commands that can clutter it. 49
You can set the HISTCONTROL controlled variable to ignore dups, which stands for ignore duplicates the 50
way the default in CentOS is, not to save duplicate commands in history. 51
So I'm modifying the variable: HISTCONTROL 52
=ignoredups. 53
ignoredups written in lowercase letters. 54
And I'm running another command multiple times. 55
Let's see the history. 56
It was saved only once. now to use both the ignorespace and ignoredups features set the HISTCONTROL 57
variable to ignore both like is the default in Ubuntu. 58
It will save nor the commands that are prepended with a white space, neither duplicates. Note that bash 59
variables you set will only persist for the current session. 60
After you log out or restart your system the settings will be lost. To save the settings 61
after the system reboots or the user logs out you have to add the settings to .bashrc file 62
in the user's home directory. 63
We'll talk more about that when we discuss editors and bash scripting. For the moment 64
the easiest way for you to do that is by running echo, the setting you want to save, 65
HISTCONTROL=ignoreboth 66
>> symbol and the path to the file .bashrc 67
This command has appended this string to the end of the file.