Translate

Archives

Bash Hash Builtin

The Bash shell hash builtin maintains a hash cache (table) containing the full pathname of previously executed commands that are on your PATH environmental variable. If the command is in the hash table, it just executes it without searching for it in the various path components.

SYNTAX
      hash [-r] [-p filename] [name]

OPTIONS
      -r   Reset (causes the shell to forget all remembered locations)
      -p   Use filename as the location of name (don't search $PATH)


If no arguments are given, information about remembered commands is printed. The return status is zero unless a name is not found or an invalid option is supplied.

The hash table also maintains a count of the number of times each command was invoked so far in that shell.

$ hash
hits	command
   2	/usr/bin/grep
   1	/usr/bin/tput
   1	/usr/bin/sftp
   1	/usr/bin/ssh
   1	/usr/bin/ls
$


The contents of the hash cache are not propagated to a new instance of Bash.

If you wish to reset the hit count for a single entry, i.e. grep, from the hash cache, just do the following:

$ hash grep
$ hash
hits	command
   0	/usr/bin/grep
   1	/usr/bin/tput
   1	/usr/bin/sftp
   1	/usr/bin/ssh
   1	/usr/bin/ls
$


The Korn shell (ksh) also has a hash builtin. Do not confuse the two. The Korn Shell hash builtin is an alias for it’s alias -t command which forces a tracked alias, i.e. full pathname, for a specific command.

Comments are closed.