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.
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 entire “PATH"
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:
Options | Description |
-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 | Display the information that can be used again as input for another program. |
-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. |
Note: This blog is mainly referenced from Javatpoint Bash Hash Commands.
FAQ
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.
hash -r
Recent Articles on Linux Operating System
- Directory in Linux Define | Linux Directory & its Commands
- Explain the chmod command in Linux | Linux chmod command
- Linux User Management || User Management in Linux
- Linux Computer Network Advanced Command | Network Command
- Redirection in Linux I/O| Linux I/O Redirection
- CronTab and Job Scheduling in Linux | Make CronTab Project
- Linux Firewall Unlock Rules with Firewall-cmd Tutorial
- netstat command in Linux | Linux netstat command
- SSH Command Full Guide with Practical | Linux SSH Service
- awk command Guide | How to arrange the output of the file in Linux
- sed command Full Guide Tutorial | Linux sed Command
- Iptables commands Full Guide: How to make our own Firewall
Recent Articles on Computer Networks
- Introduction to Computer Networking | What is Computer Network
- What are Topology & Types of Topology in Computer Network
- What is FootPrinting in Cyber Security and its Types, Purpose
- Introduction to Cloud Computing | What is Cloud Computing
- Distributed Shared Memory and Its Advantages and Disadvantages
- What is a VPN? How does a VPN Work? What VPN should I use?
- What is an Internet and How the Internet Works
- What is a Website and How Does a Website or web work?
- Introduction to Virus and Different Types of Viruses in Computer
- What is TCP and its Types and What is TCP three-way Handshake
- What is the UDP Protocol? How does it work and what are its advantages?
- What is an IP and its Functions, What is IPv4 and IPv6 Address
- What is MAC Address and its Types and Difference MAC vs IP
- What is ARP and its Types? How Does it Work and ARP Format
- Sessions and Cookies and the Difference Between Them
- What is ICMP Protocol and its Message Format?
- What is Big Data? Characteristics and Types of Big Data
- Disciplines of CyberSecurity | What are the goals of CyberSecurity?
- What is Firewall, Features, Types and How does the Firewall Work?
- Network Scanning, Types, and Stealth Scan in Computer Network
- Cryptography and its Types in Ethical Hacking
- Tor Browser and How Does It Work | Onion Router Tutorial
- Proxy Server, Advantages, Difference between Proxy Server & VPN
- DHCP Protocol and What Are the Pros and Cons of DHCP
- Intrusion Detection System(IDS) and What are the types of IDS
- Domain Name Server, How Does It Work, and its advantages
- Telnet: Introduction, How Does it Work, and Its Pros and Cons
- SOC: Introduction, Functions performed by SOC, and its Pros
- What is SIEM? | What is the Difference between SIEM and SOC?
- Application Layer in OSI Model | OSI Model Application Layer
- What is SSL Protocol or SSL/TLS and SSL Handshake, and Architecture of SSL
- What are Servers, how do they work, and its different Types
- Network Devices-Router, Switch, Hub, etc in Computer Network
- Connection Oriented and Connection-less Services in Network
- Physical Layer in OSI Model | OSI Model Physical Layer
- Presentation Layer in OSI Model | OSI Model Presentation Layer
- Session layer in OSI Model | OSI Model Session layer
- Transport Layer in OSI Model | Computer Network Transport Layer
- Network Layer in OSI Model | OSI Model Network Layer
- Data Link Layer in OSI Model | OSI Model Data Link Layer
- Block Diagram of Communication System with Detailed Explanation