In this video, you'll learn about bash history.
So bash registers recently used commands, and that makes it easy to reuse them.
You can type history for an overview.
Commands are kept in the .bash history file in your home directory, and that makes them
available beyond sessions.
In bash, there is the histsize and histfilesize variables, which are used to define the number
of entries that are kept in history.
By default, both are set to 1000, and that means that you keep your history even across
different reboots.
There are different ways to repeat commands from history.
The easiest way is the up arrow key, which allows you to scroll backwards through history.
Control-R performs a reverse search based on the pattern that you've entered.
An exclamation mark, NN, repeats a command from history based on its number.
Alternatively, use Control-A to repeat the last command that starts with letter A.
We also have a couple of convenient commands to work with history.
History minus W synchronizes the current history from memory to history file.
I like to do that when I think that the next thing that I might be doing might crash my
system, because if your system crashes, history will not be updated.
History minus C will clear the current history.
That will remove everything, and there's not often a very good reason to do so.
Maybe what you're more interested in is history minus D to remove a specific line from the
current history.
Let me show you.
To start it, I'm typing history, and there you can see that, oh, we've typed 48 commands
already.
Now, let's say I want to repeat command number 22.
This is really one of my favorites.
In history, you look up the command number, and then particularly if the command is long
enough, just execute it again using the exclamation mark.
Another way to execute commands from history is by doing exclamation mark M, but hey, exclamation
mark M is going to repeat the last command that started with an M, but are you for sure
which command it was?
Well, in this case, I am, because I just used the man command, but you won't always be sure,
so that's kind of dangerous.
Then there is control R, and control R is allowing you to do the reverse I search.
So start typing.
I'm typing pass, and what do we see?
We're searching backwards in history for a line that contains the key sequence that I've
just entered, and if this is not the one and you want to repeat the search through the
previous one, then control R again and control R again until you're happy, and then you press
enter to repeat it.
Now, another thing that I like a lot is to remove commands.
Let's say that by accident I've typed my password, oh no, secret, that was my password,
but I didn't want to type my password on clear screen.
Notice by the way that it's taking a long time to get my console back.
I'm using control C to interrupt that.
Now what happens when I type history?
Well, we can see item number 51.
That's an item that I don't want to have in my history, so let me do a history minus D
on 51 to remove it.
Then echo $hist tab tab.
I'm using tab completion to show all items, all variables that start with hist, and what
I care about is hist size and hist file size.
So as you can see, hist size is written to 1,000 and hist file size is also written to
1,000.
So when I use cat on dot bash underscore history, I can see my history file and that
is what is currently written to the history file.
Now interesting, let's investigate what is going on.
When I do my cat dot bash underscore history pipe WC minus L, I see the number of lines
in my history file, 27.
When I use history, I see 57.
What explains the difference?
Well, the difference is that not all of my history has been synchronized to my history
file.
That will only happen when you close your current shell or when you use history minus
W.
So if now I'm repeating my cat dot bash history pipe WC minus L, I can see a total of 58 commands
in history.
One thing to be aware of.
If you are working from multiple terminals, then the history file will be updated when
you close one of these terminals, and that means that your commands might be completely
out of order.
So that is a problem to be aware of with the multiple terminal environment.