#Bash 的 hash 命令
hash [OPTION]... [NAME]...
功能
记录或打印程序位置,执行被记录的命令时不再进行查找。
Bash 在默认配置下,执行一次命令后会自动进行记录。
类型
Bash 内置命令。
参数
OPTION- 选项:-d- 移除NAME的记录-l- 以命令行的格式打印记录列表-p pathname- 使用PATHNAME作为NAME的完整路径-r- 移除所有记录-t- 查看记录
NAME- 要查看或记录的命令列表
#示例
查看记录
$ hash -l # 列出记录
builtin hash -p /usr/bin/cp cp
builtin hash -p /bin/ls ls
builtin hash -p /usr/bin/cat cat
$ hash -t ls # 查看 ls 的路径
/bin/ls
添加记录
$ type arch # 查看命令的类型,返回命令的路径
arch is /usr/bin/arch
$ hash arch # 记录位置
$ type arch # 再次查看命令的类型,显示命令被哈希缓存,不再从 PATH 中进行查找
arch is hashed (/usr/bin/arch)
指定记录
$ hash -p /path/to/my_cmd my_cmd
$ type my_cmd
my_cmd is hashed (/path/to/my_cmd)
移除记录
$ hash -d my_cmd
$ type my_cmd
-bash: type: my_cmd: not found
#手册
hash: hash [-lr] [-p pathname] [-dt] [name ...]
Remember or display program locations.
Determine and remember the full pathname of each command NAME. If
no arguments are given, information about remembered commands is displayed.
Options:
-d forget the remembered location of each NAME
-l display in a format that may be reused as input
-p pathname use PATHNAME as the full pathname of NAME
-r forget all remembered locations
-t print the remembered location of each NAME, preceding
each location with the corresponding NAME if multiple
NAMEs are given
Arguments:
NAME Each NAME is searched for in $PATH and added to the list
of remembered commands.
Exit Status:
Returns success unless NAME is not found or an invalid option is given.