Hash Commands in Bash Scripting | Bash hash Commands

In this blog, we will learn about the Hash Commands in Bash Scripting. We will see all the options that are present in Linux. Now, let’s get started with the blog.

Table of Contents

Introduction to Hash Commands in Bash

A hash is a built-in command of the bash shell on UNIX-like operating systems that is used to list a hash table of recently run commands. It is utilized for manual adjustments, resets, and viewing of the bash path hash. Every time we request to see them, it displays the locations of recently run apps. Each command name’s full pathname is provided.

In other words, the shell begins looking for any command that is executed without specifying its path among the folders that are mentioned in the path variable. The position of the command is stored in a hash table when it is received by Bash so that it can remember where it is. Bash then stops looking for the command again and starts looking in the table to find its placement. It speeds up the execution of the command.

The shell won’t be able to locate the command if it is moved after registering its location in a table. To obtain the command data in this instance, a thorough search of all the directories in the path is carried out.

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

Syntax:

hash [-l] [-r] [-p pathname] [-d] [-t] [command_name . . .]

The entire pathname of each “command_name” can be found and remembered using the syntax mentioned above. It displays information about previously used commands and their locations if there are no arguments.

Hash Commands in Bash Scripting

A) Exit Status

The hash command returns ‘0’ for success. A value other than zero means that the “command_name” is not found or an invalid option is given.

B) Listing Bash hash Table

The “hash" command in Linux is used to display or clear the command hash table. The hash table is a data structure used by the shell to quickly look up the paths of executable files (commands) that have been run recently in the current session.

This helps improve the speed of command execution by avoiding the need to search the entirePATH" every time a command is run.

┌──(kali㉿kali)-[~/Downloads/Bash/Conditional_Statement]
└─$ hash
1password2john=/usr/bin/1password2john
2to3-2.7=/usr/bin/2to3-2.7
7z=/usr/bin/7z
7z2john=/usr/bin/7z2john
7za=/usr/bin/7za
7zr=/usr/bin/7zr

Here, you can see the hash table of my last used commands in the system.

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

C) Clearing the hash Table

To clear the entire command hash table, you can use the -r option:

┌──(kali㉿kali)-[~/Downloads/Bash/Conditional_Statement]
└─$ hash -r
                                                                                                                                                                                                                                           
┌──(kali㉿kali)-[~/Downloads/Bash/Conditional_Statement]
└─$ hash   

From the above command, this will clear all entries from the hash table, causing the shell to rebuild the table the next time a command is executed.

D) Adding a Command Path and Name to the Bash Hash Table

The hash table can be expanded with items that can be used repeatedly in the shell. The hash table only lives in the shell’s current active session, it should be kept in mind. When a new shell is opened, bash will build a new hash table based on the commands that were run in that shell.

Bash starts building a hash table as soon as we start executing the first command. We can use the -p option, the path, and then the name to add a command to the hash table.

┌──(kali㉿kali)-[~/Downloads/Bash/Conditional_Statement]
└─$ hash -p /usr/local/bin ls

E) Delete Specific commands from the hash table

We also have the 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 -d bash

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

Conclusion

Other options we can see in the hash command are:

OptionsDescription
-dForget the remembered locations of command_name.
-lDisplay the information that can be used again as an input for another program.
-pUse pathname as the full path of command_name.
-rDisplay the information that can be used again as input for another program.
-tPrint the remembered location of each command_name. If multiple command_names are given there, precede each location with corresponding command_name
command_nameEach command_name specified is searched for in the path environment variable, and if found, is added to the list of remembered commands.
All Bash Hash Options Command

Note: This blog is mainly referenced from Javatpoint Bash Hash Commands.

FAQ

What are hash commands in Linux?

The “hash" command in Linux is used to display or clear the command hash table. The hash table is a data structure used by the shell to quickly look up the paths of executable files (commands) that have been run recently in the current session.

How to clear the hash table in Linux?

hash -r

Recent Articles on Linux Operating System

  1. Directory in Linux Define | Linux Directory & its Commands
  2. Explain the chmod command in Linux | Linux chmod command
  3. Linux User Management || User Management in Linux
  4. Linux Computer Network Advanced Command | Network Command
  5. Redirection in Linux I/O| Linux I/O Redirection
  6. CronTab and Job Scheduling in Linux | Make CronTab Project
  7. Linux Firewall Unlock Rules with Firewall-cmd Tutorial
  8. netstat command in Linux | Linux netstat command
  9. SSH Command Full Guide with Practical | Linux SSH Service
  10. awk command Guide | How to arrange the output of the file in Linux
  11. sed command Full Guide Tutorial | Linux sed Command
  12. Iptables commands Full Guide: How to make our own Firewall

Recent Articles on Computer Networks

  1. Introduction to Computer Networking | What is Computer Network
  2. What are Topology & Types of Topology in Computer Network
  3. What is FootPrinting in Cyber Security and its Types, Purpose
  4. Introduction to Cloud Computing | What is Cloud Computing
  5. Distributed Shared Memory and Its Advantages and Disadvantages
  6. What is a VPN? How does a VPN Work? What VPN should I use?
  7. What is an Internet and How the Internet Works
  8. What is a Website and How Does a Website or web work?
  9. Introduction to Virus and Different Types of Viruses in Computer
  10. What is TCP and its Types and What is TCP three-way Handshake
  11. What is the UDP Protocol? How does it work and what are its advantages?
  12. What is an IP and its Functions, What is IPv4 and IPv6 Address
  13. What is MAC Address and its Types and Difference MAC vs IP
  14. What is ARP and its Types? How Does it Work and ARP Format
  15. Sessions and Cookies and the Difference Between Them
  16. What is ICMP Protocol and its Message Format?
  17. What is Big Data? Characteristics and Types of Big Data
  18. Disciplines of CyberSecurity | What are the goals of CyberSecurity?
  19. What is Firewall, Features, Types and How does the Firewall Work?
  20. Network Scanning, Types, and Stealth Scan in Computer Network
  21. Cryptography and its Types in Ethical Hacking
  22. Tor Browser and How Does It Work | Onion Router Tutorial
  23. Proxy Server, Advantages, Difference between Proxy Server & VPN
  24. DHCP Protocol and What Are the Pros and Cons of DHCP
  25. Intrusion Detection System(IDS) and What are the types of IDS
  26. Domain Name Server, How Does It Work, and its advantages
  27. Telnet: Introduction, How Does it Work, and Its Pros and Cons
  28. SOC: Introduction, Functions performed by SOC, and its Pros
  29. What is SIEM? | What is the Difference between SIEM and SOC?
  30. Application Layer in OSI Model | OSI Model Application Layer
  31. What is SSL Protocol or SSL/TLS and SSL Handshake, and Architecture of SSL
  32. What are Servers, how do they work, and its different Types
  33. Network Devices-Router, Switch, Hub, etc in Computer Network
  34. Connection Oriented and Connection-less Services in Network
  35. Physical Layer in OSI Model | OSI Model Physical Layer
  36. Presentation Layer in OSI Model | OSI Model Presentation Layer
  37. Session layer in OSI Model | OSI Model Session layer
  38. Transport Layer in OSI Model | Computer Network Transport Layer
  39. Network Layer in OSI Model | OSI Model Network Layer
  40. Data Link Layer in OSI Model | OSI Model Data Link Layer
  41. Block Diagram of Communication System with Detailed Explanation
Write blogs related to Ethical hacking, Computer networks, Linux, Penetration testing and Web3 Security.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top
0 Shares
Share via
Copy link