Skip to main content
Commands

Use hash Command in Linux

The hash command in Linux allows you to quickly get the list of commands you executed in the current session.

Sagar Sharma

Warp Terminal

Most Linux users use the history command to get the list of previously used commands but that's not the only way.

You can use the hash command which is quite faster than the history command and shows the full path of the command (where the command is stored).

So in this tutorial, I will walk you through how you can use the hash command.

But before that, let's have a look at how different it is than the history command.

The difference between history and hash

The whole purpose of using the hash and history command is different.

When you execute the hash command, it will show the list of previously executed commands but are limited to the current shell session as it stores the data in the hash table.

On the other hand, you don't have such limitation in the history command as it stores the previously executed commands in bash_history file.

The main advantage of using the hash command is it fetches data from the hash table which eventually reduces the response time compared to the history command.

The big con of using the hash command is that it does not include built-in bash commands like echo, cd, etc., so you won't get the full list of previous commands.

Whereas the history command will include every command, function, and executables in the list.

Which raises a question. Should you really be using it? Well, if you are dealing with multiple shell sessions and want to treat them independently and the response time also matters a lot, then the hash command is a great option for you.

How to use the hash command

To learn the use of any command, knowing the command syntax first is a good idea, and that's the reason why I'm starting with the syntax of hash:

hash [options] [command]

Here,

  • [options] is used to change the default behavior of the hash command as per your liking such as clearing the hash table.
  • [command] is where you enter the name of the command that you want to be hashed or unhashed. In simple terms the name of the command you want to add/remove from the hash table.

Now, let's have a look at examples of using the hash command one by one.

1. Access the hash list

To access the hash list, all you have to do is execute the hash command without any options as shown here:

hash

If you have used multiple commands in the current shell, it will show the list of previous commands:

You'd see 2 columns: hits and command. Here, the hits column will show the number of how many times the command was used and the command column shows the path of the command.

But if you execute the hash command in the fresh shell session, it will show the following command:

hash table empty

You can also check the command individually if it is hashed or not using the type command:

type <command>

For example, here, I checked for the whoami command if it is hashed or not:

type whoami
Check if the command is hashed or not

If the command is not hashed, then it will show a different output:

command is not hashed

2. Add commands to the hash table (without execution)

To add one or more commands to the hash table, all you have to do is append the command names to the hash command as shown:

hash command1 command2 command3 commandN

For example, here, I added 3 commands to the hash list:

hash neofetch apt topgrade
add commands to the hash table

I'm sure you must be wondering why the zeroes in the above image are highlighted. If you remember, the hits column shows how many times the command was executed.

But as I added the commands without executing them, it is showing 0 here.

3. Delete entries from the hash table

If you want to remove a command entry from the hash table, all you have to do is use the -d flag followed by one or more command names as shown:

hash -d command_1 command_2 command_3

If I want to remove the xargs and grep commands from the hash table, then I will be using the following:

hash -d xargs grep
delete commands from the hash table

4. Reset hash table

To reset the hash table, all you have to do is use the -r flag with the hash command:

hash -r
reset the hash table in Linux

5. Setup path to rename commands (kind of)

🚧
This will only be effective for the current shell session.

This is similar to setting up an alias where you can execute a command with a different name.

Using the hash command, you can give a path to the command and use a different name to trigger the command.

Let me give you an example.

You use the date command to know the current date but if you execute d instead of the date, it will give you an error:

command not found

So I want the date command to be executed with d.

First, you have to know the path of the command which you want to rename or create a substitute for:

type <command_name>
know the path of the command

Once you know the path, use it with the hash command as shown:

hash -p /path/to/command <renamed-command>

As I wanted to execute the date command when executed d, I used the following:

hash -p /usr/bin/date d
rename command using the hash command

There you go!

My take on history vs hash

I personally use the history command as I can compromise little speed (which is not even noticeable) and it can also store built-in commands as well as script executions.

The history command is also customizable as you can decide how many commands you can store and you can also execute previously used commands easily.

Let me know what's your take on this.

If you still have any queries, feel free to leave a comment.

Sagar Sharma