Beginners Guide for Hash Command in Linux

Linux TLDR
Last Updated:
Reading time: 5 minutes

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

DescriptionHash
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisiteshash
Internet RequiredNo

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:

Checking the hash list

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 ListHash 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:

Checking the hash list

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:

Checking the type of hashed command

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:

Listing the hashed command in reusable format

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.

πŸ“
Aside from the mentioned commands, you can add any command to the hash list by specifying its absolute path and command name with the β€œ-p” flag; however, if the specified path is invalid, an error will occur.
$ hash -p /usr/bin/ps ps

Output:

Importing the hashed command

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:

Removing an item from the hash list

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:

Removing multiple items from the hash list

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:

Clearing the hash list

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:

Trying to add a built-in command to the hash list

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:

Listing the built-in commands

Although, you can add them using the β€œ-p” flag with their absolute path and command name.

Adding the built-in command to the hash list

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.