The hash
command maintains a hash table, which has the used command's path names. When you execute a command, it searches for a command in the variable $PATH
. But if the command is available in the hash table, it picks up from there and executes it. Hash table maintains the number of hits encountered for each commands used so far in that shell.
#!/usr/bin/env bash
# Bash hash command
#
# hash command maintains a hash table, which has the used command's path names.
# When you execute a command, it searches for a command in the variable $PATH.
# But if the command is available in the hash table, it picks up from there
# and executes it. Hash table maintains the number of hits encountered
# for each commands used so far in that shell.
$ hash
# hits command
# 1 /usr/bin/cat
# 2 /usr/bin/ps
# 4 /usr/bin/ls
##
# Example
#
# Check for git command, exit script if not found.
##
$ hash git 2>&- || { echo -e >&2 "n git not installed! n"; exit 1; }
### NOTE: For csh, tcsh or zsh shells the `hash` command is not available. The `hash` equivelant is `rehash`.
rehash