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.
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.
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 TableWe can display the hash table for the current session by invoking hash without any argument.
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:
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.,
We can also print the remembered locations of commands which are used during the session by using the -t option.
We can also print the locations of multiple commands separated by spaces, i.e.,
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'.
After adding the /home/bash.sh to the hash table with a mapped name 'bash', we can directly invoke it by the name 'bash':
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:
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.
Here, it can be seen that the hash table is cleared successfully by using the -r option.