hash Command in Linux



The hash command is an integral part of the Bash shell, which is a common command-line interface used in Unix and Linux systems. You don't need to install anything extra to use it.

  • The hash command helps manage a special table called the hash table. This table keeps track of the paths to the commands you use frequently.
  • You can use the hash command to see which commands are stored in the hash table and their associated paths. When you run a command, the hash command can store the path to that command in the hash table.

Instead of searching through all directories every time you run a command, the hash command can quickly provide the path from the hash table, making the process faster.

Imagine you frequently use the ls command to list directory contents. Without the hash table, every time you type ls, the system searches through all directories in your PATH to find it.

With the hash table, once you run the ls command, its path is stored, and the next time you type ls, the system retrieves the path directly from the hash table, saving time.

Importance of Using the hash Command

The following points highlight the importance of using the hash command −

  • Reduces disk I/O operations − Disk I/O operations involve reading from or writing to the disk. By storing command paths in memory (the hash table), the hash command reduces the need for repeated disk access.
  • Displays the command path − The hash command can show you the paths of commands stored in the hash table. This is useful for verifying which version of a command is being executed, especially if you have multiple versions installed. This also helps debug and ensure that the correct executable is used.
  • Manages the hash table − The hash command provides options to list, add, and remove entries from the hash table. This gives you control over the command lookup process, allowing you to optimize it according to your preference.
  • Improves user experience − Faster command execution and reduced disk I/O contribute to a streamlined and more responsive command-line experience.
  • Provides command usage data − The hash command can show how many times each command has been used since it was hashed. This can help you understand your command usage patterns and optimize your workflow.

Table of Contents

Here is a comprehensive guide to the options available with the hash command −

Syntax for the hash command

The following is the general syntax for the hash command −

hash [option] [command] 

Options Available for the hash Command

The following table highlights the flags / options that are regularly used with the hash command −

Tag Description
l (List): Displays a list of information that can be reused as input for another program.
r (Reset) Removes all entries and clears the hash table.
p (Pathname) Allows adding or modifying entries in the hash table with a full pathname.
t (Type) Displays the location of the specific commands.
d (Delete) Removes the remembered locations of each command in the hash table
v (Verbose) Provides detailed information about the hash table operations.
f (Force) Forces the hash table to update its entries.
s (Statistics) Displays statistics about the hash table, such as the number of hits and misses.

Examples of hash Command in Linux

In this section, we'll look at practical examples of the hash command using the various options we've described above −

Access the Hash List

To access the hash list, you can simply execute the hash command without any options, as shown −

hash
hash Command in Linux1

If you have previously executed multiple commands in the current shell, it will show the list of commands.

In the above output, the hits column shows the number of times the command was used, and the command column shows the path of the command. However, if you execute the hash command in a new shell session, it will show an empty output −

hash Command in Linux2

Check if the Command is Hashed

To check if a command is hashed or not, you can simply use the type command followed by the command you want to check −

type cat
hash Command in Linux3

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

hash Command in Linux4

Add Commands to the Hash Table

To add a single or multiple commands to the hash table without executing them in your current shell, you can append the command names to the hash command as shown −

hash whoami grep apt
hash Command in Linux5

Deleting Entries from the Hash Table

To remove a command entry from the hash table, you can simply use the “-d” flag alongside one or more command names, as shown −

hash -d  grep apt
hash Command in Linux6

Setup Path to Rename Commands

With the hash command, you can give a path to the command and use another name to trigger the command. This is quite similar to setting up an alias where you can run a command with a different name.

Let's paint a scenario where we want to use the date command to know the current date, but if we execute d instead of the date, it gives an error −

hash Command in Linux7

So, if we want the date command to be executed with d. First, we'll have to know the path of the command that we want to rename or create a substitute for −

type date
hash Command in Linux8

Once we've identified the path, we'll use it with the hash command as shown −

hash -p /usr/bin/date d
hash Command in Linux9

Now, we can run the d option instead of the date command to display the current date −

hash Command in Linux10

Checking Return Values

You can check the return values of hash commands when you are using them in a script. The following table shows the return values of the hash −

Value Description
0 Successful execution
Otherwise Command not found, an error occurred

You can simply access the return values provided by the hash command through ‘$?’, which is a special shell variable that holds the exit status of the most recent command.

To demonstrate this, we’ll create a script file, for instance, check_ping.sh, and add the following contents to the script file.

#!/bin/bash
# Check if the 'ping' command is available.
hash ping
# Capture the return value
return_value=$?

# Print the return value
echo "Return value: $return_value"

# Check the return value and print output accordingly.
if [ $return_value -eq 0 ]; then
	echo "The 'ping' command is available."
else
	echo "The 'ping' command is not found or an error occurred."
fi

Next, we’ll make the script executable using the following command −

chmod +x check_ping.sh

Now, we'll run the script −

./check_ping.sh
hash Command in Linux11

As you can see, the return value in the above output shows 0, indicating that the ping command is available.

List All Hashed Commands

To display a list of currently hashed commands in the hash table, you can use the -l flag with the hash command −

hash -l
hash Command in Linux12

Display the Location of Commands

To display the location of a specific hashed command, open your terminal and run the following command −

hash -t wget
hash Command in Linux13

To also display the location of multiple commands, you can use the following syntax −

hash -t cat grep
hash Command in Linux14

Add a Command with a full Pathname

You can use the hash command with the “-p” flag to add a command to the hash table. To achieve this, you can use the following syntax −

hash -p /usr/bin/ls ls

This command adds or modifies the entry for ls with the full pathname. /usr/bin/ls.

hash Command in Linux15

Reset the Hash Table

You can clear all the commands in the hash table by running the hash command with the “-r” option −

hash -r
hash Command in Linux16

Conclusion

The hash command is an essential tool for optimizing command execution in Unix and Linux environments. By maintaining a hash table of frequently used commands and their paths, this command significantly enhances system performance by reducing unnecessary disk I/O operations, speeding up command retrieval, and more.

Whether you need to list hashed commands, add new entries, or reset the table, the hash command provides a straightforward approach to streamline your workflow.

Advertisements