hash

Manage the command path cache.

Source: src/execution/builtins.f90:2665-2829

Synopsis

hash [-lr] [-p path] [-dt] [name ...]

Description

The hash builtin remembers the full pathnames of commands, avoiding PATH searches on subsequent invocations. This speeds up command execution.

Options

OptionDescription
-rClear (reset) the hash table
-lList in reusable format
-dDelete specific names from hash
-tPrint path for each name
-p pathUse path for name (don't search)

Usage

View Hash Table

hash
# hits    command
#    5    /usr/bin/git
#    3    /usr/bin/ls
#   12    /usr/bin/grep

List in Reusable Format

hash -l
# hash -p /usr/bin/git git
# hash -p /usr/bin/ls ls

Clear Hash Table

hash -r

Delete Specific Entry

hash -d git

Get Path for Command

hash -t git
# /usr/bin/git

Force Path

hash -p /opt/custom/git git

Behavior

Automatic Hashing

When you run a command, fortsh:

  1. Checks the hash table
  2. If not found, searches PATH
  3. Adds found path to hash table
  4. Tracks hit count

When to Clear

Clear the hash table when:

  • Commands move or are reinstalled
  • PATH changes significantly
  • You get "command not found" for known commands
# After installing new software
hash -r

# After modifying PATH
export PATH="/new/path:$PATH"
hash -r

Examples

Check Cached Path

hash -t python
# /usr/bin/python

# After installing new Python:
hash -d python
hash -t python
# /usr/local/bin/python

Performance Comparison

# First run: searches PATH
time ls > /dev/null

# Subsequent runs: uses cached path
time ls > /dev/null   # Faster

Script Optimization

# Pre-hash frequently used commands
hash git make gcc

# Clear stale entries
hash -r

Check If Cached

if hash -t mycommand 2>/dev/null; then
    echo "mycommand is cached"
fi

Implementation

Source: builtins.f90:2665-2829

Hash entries are stored with:

  • Command name
  • Full path
  • Hit count
  • Timestamp

Exit Status

StatusCondition
0Success
1Name not found (with -t or -d)

Notes

  • Hashing is automatic for external commands
  • Builtins and functions are not hashed
  • Relative commands (with /) are not hashed
  • Hash is per-session (not persistent)

Comparison

CommandPurpose
hashManage path cache
typeShow command type and path
whichFind command in PATH
command -vPOSIX command location

See Also

  • type - Show command type
  • command - Run bypassing functions