You might already be aware of the Linux shell’s default behavior of capturing each user’s executed commands and storing them in the “~/.bash_history
” file, so later you can view the history list using the history command.
Although, history is not the only location where your executed commands are stored, a Linux shell like Bash, Ksh, or Zsh also stores the user’s executed commands in a list known as a hash list.
In this article, you will learn what the hash list is, the differences between it and the history list, and how to use its command-line tool to manage records in the hash list in Linux.
Tutorial Details
Description | Hash |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | hash |
Internet Required | No |
What is a Hash List in Linux?
As I’ve told you previously, the history file is not the only place where your executed commands are stored.
Most Linux shells also store your executed commands with their paths in a list known as the hash list.
Execute the following command to view the saved records in your existing hash list:
$ hash
Output:
As you can see from the above picture, each of the “mv
“, “echo
“, and “which
” commands were previously executed in the current shell session and stored in the hash list.
This way, your shell avoids wasting time searching for commands (or executable files) in the $PATH variable every time you type a command by caching the results in memory.
Is the Data Persistent in the Hash List?
One thing to keep in mind is that each shell session keeps its own hash list, which is valid as long as the session is active; once the session is closed, all captured commands will be flushed.
In addition to that, the hash list will also be flushed in the event that invalidates the result, like a modification in the $PATH
variable.
Difference Between the Hash and History Lists?
The hash and history lists are totally different in terms of working, but as both capture user-executed commands, this might lead to confusion.
The following is a common difference between them:
History List | Hash List |
---|---|
Capture user executed commands along with the specified options and arguments in the “~/.bash_history ” file. | Capture user executed commands and the executable paths in a hash list. |
The front-end command to manage this list is the history command. | The front-end command to manage this list is the hash command. |
Keep the record of executed commands from all shell sessions in a single history file. | Keep the record of executed commands in the respective shell session hash list, valid until the session is active. |
The goal of this list is to give users a view of all commands executed in the past. | The goal of this list is to save time by returning the previously entered command from the hash list instead of searching in the $PATH variable. |
Store all types of built-in keywords, functions, and executables in the list. | Does not store the built-in keywords like echo, if, etc. |
I think that would be enough for the introduction; let’s see the command’s usage.
Syntax of the Hash Command
The hash command is used to manage the hash list that takes two arguments: one is the option and the other is the command name; both are optional.
$ hash [OPTION] [COMMAND]
Displaying the Hash List
The simplest way to use this tool is to run the command without any flags to get a list of hashed (or cached) commands.
$ hash
Output:
From the above picture, you can see two columns. The first column is for “hits
“, which basically records the number of times the respective command was executed; for example, the ps command was executed the most.
And the second “command
” column stores the paths of all the commands and executable files. When you run commands or executable files in the future, the shell will look them up in this list instead of searching for them in the $PATH variable.
Apart from that, whenever the command is hashed, their output for type command will be changed to “<command> is hashed
“, as shown.
$ type mv
Output:
Lastly, this list is only valid for the current shell session; if you launch a new shell session, you will have an empty hash list.
Although, you can import the results of this session into a new session by following the next section,
Exporting the Hash List to a New Shell Session
As I said before, the hash list only persists the data for the current shell session. If you open a new shell session, such as by opening a new terminal window or tab, a new hash list will be created.
However, you can use the “-l
” flag to list the hashed command in a reusable format that can be used to import the hash list from one shell session to another.
For example, execute the following command in your main shell session from where you want to export your hash list:
$ hash -l
Output:
Now, from the above list, except for “builtin
” (representing the type), you can copy the rest of the command and paste it into your new shell session, as shown.
-p
” flag; however, if the specified path is invalid, an error will occur.$ hash -p /usr/bin/ps ps
Output:
This question might come to your mind that you can directly execute the command to add it in the hash list instead of following the above method.
It’s true, you can do that, but this approach comes in handy, especially when you want to add a certain command to the hash list whenever you open a new shell session by adding these reusable commands to your shell configuration file.
Now, let’s see how you can delete an entry from the hash list.
Deleting an Entry from the Hash List
You can easily remove an entry from the hash list by using the “-d
” flag with the command name from the hash list.
For example, the following command will remove the mv command from the hash list:
$ hash -d mv
Output:
Now, you are not limited to removing one item at a time; you can specify multiple commands using space as a separator to remove them from the hash list.
$ hash -d cp whoami
Output:
Clearing the Hash List
The “-r
” flag will remove all the entries from the hash list, forcing your shell to search for commands or executable files in the $PATH variable.
$ hash -r
Output:
Bonus Tip: Checking the List of Built-in Commands
I previously stated this, but let me reiterate that built-in commands such as alias, echo, bg, and so on will not be added to the hash list.
$ echo
$ bg
$ cp
$ hash
Output:
Apart from the cp command, as shown in the preceding image, neither the echo nor the bg built-in commands were added to the hash list.
Execute the following compgen command to get the complete list of built-in commands:
$ compgen -b
Output:
Although, you can add them using the “-p
” flag with their absolute path and command name.
However, adding built-in commands to the hash list in this manner is pointless because the hash list’s purpose is to assist Bash in providing the path to a command or executable file as quickly as possible.
As the built-in command is already part of the shell itself, it doesn’t make any sense to add it to the hash list.
So, that’s all for now.
If you have any questions or queries regarding this topic, feel free to ask them in the comment section.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.