Bash is a popular built-in shell in the Unix-like operating system. And hash is the built-in command within the shell that maintains the bash path hash which is actually a hash table. By running a hash command, one can force the hash table to execute all the commands in subsequent invocations rather than the entire system’s directory search. In this guide, I will demonstrate the functionalities of the hash command on Linux within a hash table.
- Learning about the hash command in Bash.
- Learning about the syntax and return values of hash commands.
- Getting ideas about various executions on a hash table.
- For viewing hash table entries and command lookup.
- For refreshing the hash table to make the shell understand the changes.
- For forcing the shell to look for a command in the path.
- For clearing the hash table.
What is ‘hash’ in Bash?
Hash is a built-in command in the Bash shell. It plays a significant role in managing the hash table and boosting the command execution speed. The hash command displays the contents of the hash table, records the commands path, and reveals the cached commands’ path avoiding the full directory search.
The basic syntax for the hash command:
hash [option] [command]
There are several options that can be used in hash syntax. They are:
- 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.
- command: It is an optional argument to limit the options only to the mentioned ones.
Why Bash the ‘hash’ Command?
The hash command accelerates the command execution within the bash shell. Here is why you should use bash the hash command:
- To speed up command execution.
- To reduce disk I/O operations.
- To display the command path.
- To manage the hash table.
- To improve user experience.
The return values of hash commands are usually checked only when you are using them in a script. Here are the return values of the hash:
|Otherwise||Command not found, an error occurred|
You can access the return values provided by the hash command through ‘$?’, a special shell variable just after execution like the following:
The above image represents the execution of the hash command with a special shell variable for accessing return values.
hash <command> echo $?
Practical Examples of “hash” command in Linux
Hash tables have versatile implementations. You can display, list up, add, delete contents, and more within the hash table. Below I am going to share some execution of the hash table in Linux:
Example 1: Displaying the Hash Table Using the “hash” Command
If you want to display the hash table, follow the steps below accordingly:
Steps to Follow >
❶ At First, open your Ubuntu terminal.
❷ Write the following command to show the hash table:
You will see an output like the following image:The image shows that the hash table is empty.
Example 2: Listing the contents of the Hash Table Using the “hash” Command
Use the following command in the terminal to display the list of currently hashed commands that can be used as input for another program:
You can see the list of the hash table in the above image.
Example 3: Showing the Location of Commands Using the “hash” Command
To display the location of a command hashed currently, open your Ubuntu terminal & write the command below:
To display the location of multiple commands write the following command:
hash -t chmod
From the image, you can see the locations of two commands ‘chmod’ and ‘nano’.
hash -t chmod nano
- The “alias” Command in Linux [3 Practical Examples]
- The “unalias” Command in Linux [3 Practical Examples]
- The “exit” Command in Linux [5 Practical Examples]
Example 4: Adding & Naming Command Path Using the “hash” Command
Use the command option ‘-p’ to add a command to the hash table. For that, write the following command on your command prompt:
By running the ‘hash -l’ command you can check if the new item has been added or not like in the snapshot above.
hash -p /home/hash.sh bash
Example 5: Deleting Items from Hash Table Using the “hash” Command
To delete an item from the hash table, write the following command in the terminal:
Subsequently, run ‘hash -l’ to see the current list of items in the hash table. And from the above image you can see that after executing the delete operation on the ‘/usr/bin/clear’ item, it is no longer in the list of recently hashed commands.
hash -d command
Example 6: Clearing the Hash Table Using the “hash” Command
After checking all items in the hash table, you can fully clear the hash table by running the command below in the command prompt:
Check if the hash table has successfully been removed or not by running the command ‘hash’ once again. If it is done properly, you will see the output message ‘hash: hash table empty’ like the above image.
Wrapping up the whole guide you will get the key points of the ‘hash’ command in Linux that helps optimize the track of executed commands and boost up the speed and response time.
People Also Ask
- The “bash” Command in Linux [5 Practical Examples]
- The “clear” Command in Linux [3 Practical Examples]
- The “declare” Command in Linux [7 Practical Examples]
- The “export” Command in Linux [8 Practical Examples]
- The “source” Command in Linux [3 Practical Examples]
- The “echo” Command in Linux [7 Practical Examples]