hash

shell builtinLinux/Unix
The hash command is one of the most frequently used commands in Linux/Unix-like operating systems. hash Display or manipulate the bash command hash table

Quick Reference

Command Name:

hash

Category:

shell builtin

Platform:

Linux/Unix

Basic Usage:

hash [options] [arguments]

Common Use Cases

    Syntax

    hash [-lr] [-p pathname] [-dt] [name]

    Options

    Option Description
    -d Forget the remembered location of the specified name(s)
    -l Display in a format that can be reused as input
    -p pathname Use pathname as the full pathname for the specified name
    -r Forget all remembered locations
    -t Print the remembered path for each specified name

    Examples

    How to Use These Examples

    The examples below show common ways to use the hash command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    hash
    Display all remembered commands.
    hash command1 command2
    Remember the full pathnames of the given commands.
    hash -r
    Forget all remembered command locations. # Advanced Examples Advanced hash -p /usr/local/bin/command command Set the path for a specific command. hash -d command Forget the remembered location of the given command. hash -t command Print the remembered path for a specific command. hash -l Display commands in a format that can be reused as input.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The hash command in bash is used to maintain a hash table of recently executed commands, which helps improve command execution performance by storing the full path of the command to avoid PATH searches for frequently used commands. Key features of hash: 1. Performance Optimization: The primary purpose of hash is to speed up command execution by eliminating repeated PATH searches for commands that have already been found. 2. Command Tracking: Without arguments, hash displays the commands currently stored in the hash table along with the number of times each has been executed. 3. Path Management: hash allows manually specifying the full path for a command using the -p option, which is useful when you want to use a specific version of a command. 4. Hash Table Reset: The -r option clears the entire hash table, which can be helpful when command locations have changed, such as after installing software updates. 5. Selective Forgetting: The -d option allows removing specific commands from the hash table, which is useful when you know a particular command's location has changed. 6. Path Lookup: The -t option displays the stored path for a command without executing it, which can help with debugging PATH-related issues. 7. Shell Integration: As a built-in command, hash is specifically designed to work with the shell's command execution process, making it more efficient than external utilities for this purpose. The hash command is particularly useful in shell scripts or environments where the same commands are executed repeatedly. By maintaining the hash table, the shell avoids the overhead of searching through all directories in the PATH variable each time a command is executed. It's worth noting that in modern systems with fast disks and caching, the performance benefit of the hash table may be less noticeable than it was historically, but it still remains a useful feature for optimizing command execution.

    Related Commands

    These commands are frequently used alongside hash or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the hash command works in different scenarios.

    $ hash
    View All Commands