15

I was reading this post and trying out all of the commands in the first answer... I don't really have anything else better to do right now.

Anyways, I ran through them all in regards to rm. What piqued my interest is this:

root@headdesk:~# type ls
ls is /bin/ls
root@headdesk:~# hash ls
root@headdesk:~# type ls
ls is hashed (/bin/ls)
root@headdesk:~#

Running help/man hash is not very helpful, and help type isn't really either (it does not mention hash anywhere). Since I can't seem to find a relation in the man/info/help pages unless I am missing something, could someone please explain what hashing is doing in regards to the type command?

cutrightjm
  • 5,050
  • 4
  • 22
  • 32
  • 2
    your shell *remembers* where it finds a command. a `$PATH` search is brute forced otherwise, but if it finds a command it stores its location in a hash table *(supposed to be, anyway)* so it can call it up next time. if you do `PATH=$PATH` or `hash -r` it will forget all executables in the hash table. it doesnt *usually* affect anything, but it can make a difference for weird lookup tables compiled out of a list of empty files in the current directory, for instance. it used to make things a lot faster, but i dunno if its so true anymore. maybe if `$PATH` is huge,... – mikeserv Dec 27 '15 at 06:38

2 Answers2

4

It means stored in a hash table for quicker reference next time around.

Michael Durrant
  • 41,213
  • 69
  • 165
  • 232
1

In bash: just type help hash and you will get a help about the built-in command hash.

Determine and remember the full pathname of each command NAME.

What that means is that after finding the location of a command the first time (or when hash is invoked), its location is remembered (hashed).

  • Makes sense - little bit of a different question, but it says obsolete at the bottom of the `help` page for `type`. Are there any other commands that you can view if a command is hashed besides `type`? – cutrightjm Dec 27 '15 at 20:26
  • @ekaj If you do `hash cat` to get cat hashed, `hash -t cat` will print the hashed directory. If `cat` is not hashed `hash -d cat`, the command `hash -t cat` will print `not found`. The `Obsolete` part apply to the command `typeset` which happens to also be printed at the end of `type` in older bash help versions. Does not apply to `type`. –  Dec 27 '15 at 21:19