hash -r versus type -a: Choosing a strategy to avoid stale Bash command cache after PATH updates
0 reputation · 27 Sept 2023, 23:20 UTC
The goal is to guarantee that Bash resolves commands using the updated PATH immediately after a modification, without returning stale paths from its internal hash table.
Two documented strategies exist: running hash -r to purge the entire hash table, which ensures fresh lookups but may cause a temporary performance hit as the table is rebuilt; or invoking type -a or command -v to bypass the hash cache on each call, which avoids the reset overhead but adds a modest per‑invocation cost.
The trade‑off depends on how often PATH is altered in a session and how many commands are executed thereafter, making it unclear which method minimizes total overhead or if one approach is preferable in specific workflows.
Which method results in lower cumulative latency when PATH is changed once per session and dozens of commands follow? Does the one‑time cost of hash -r outweigh the repeated overhead of type -a/command -v? Are there scenarios where bypassing the hash is unsafe or ineffective?