Javatpoint Logo
Javatpoint Logo

Bash Hash Command

On UNIX-like operating systems, a hash is a built-in command of the bash shell, which is used to list a hash table of recently executed commands. It is used for views, resets, or manually changes within the bash path hash. It keeps the locations of recently executed programs and shows them whenever we want to see it. It provides a complete pathname of each command name.

In other words, when any command is executed without naming its path, the shell starts searching for that command within the directories, which are listed in the path variable. When the Bash gets that command, it keeps the location in a hash table so that it can remember the location of the command. After that, Bash starts checking the table to find the location of the command instead of looking for the command again. It makes the command run faster. However, if the command is moved after recording its location in a table, the shell will not be able to find the command. In this case, a full search of the directories in the path is performed to get the command data.

The built-in 'hash' command is responsible for maintaining the hash table. Without any switches, hash lists the previously used commands, their locations, and the number of times they have been executed during the session.

Syntax

The syntax above is used to determine and remember the full pathname of each command_name. If there are no arguments, it displays the information about previously used commands and their locations.

Options

-d Forget the remembered locations of command_name.
-l Display the information that can be used again as an input for another program.
-p Use pathname as the full path of command_name.
-r Forget all the remembered locations.
-t Print the remembered location of each command_name. If multiple command_names are given there, precede each location with corresponding command_name
command_name Each command_name specified is searched for in the path environment variable, and if found, is added to the list of remembered commands.

Exit Status

The hash command returns '0' for success. Value other than zero refers that the command_name is not found, or an invalid option is given.

Listing Bash Hash Table

We can display the hash table for the current session by invoking hash without any argument.


Hash Command

Here, the hash command displays the number of hits (calls for that command) and the command with its path during the session. The sum of all the hits is considered as the number of saved searches through the path.

Note- If a new session is opened where no command has been executed, then there will be no hash table for that session. The output will look like this:

Hash Command

If we use the -l option, then it will display a hash table in a format that can be used as an input, i.e.,


Hash Command

We can also print the remembered locations of commands which are used during the session by using the -t option.

Hash Command

We can also print the locations of multiple commands separated by spaces, i.e.,

Hash Command

Adding a Command Path and Name to the Bash Hash Table

We can add items to the hash table that can be used again in the shell. It should be remembered that the hash table only exists in the current live session of the shell. If we open a new shell, the bash will create a new hash table according to the executed commands of that shell.

As soon as we start running the first command, bash starts creating a hash table. To add a command to the hash table, we can use the -p option followed by the path and then the name. In this way, we can use the hash table as similar to an alias. Following is an example where we have added the /home/bash.sh script to the hash table with the name 'bash'.

Hash Command

After adding the /home/bash.sh to the hash table with a mapped name 'bash', we can directly invoke it by the name 'bash':

Hash Command

Bash checks the hash table for the command name to find the executable. Generally, there is no need to put the script in the path unless we want it to be available in a new shell.

Deleting an Item from the Hash Table

We also have an option to delete or forget a remembered location of commands in hash bash. We can simply use the -d option followed by the name to perform this task:

Hash Command

Here, we have deleted the /home/bash.sh from the hash table, which was mapped with a name 'bash'.

Clearing the Hash Table

To clear the hash table, we can use the -r option.

Hash Command

Here, it can be seen that the hash table is cleared successfully by using the -r option.


Next Topic#




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA