Logo
18 Aug 2018 | 1 min. (197 words)

Uncommon bash builtins: Hash

Hash

Bash doesn’t search PATH every single time you invoke a command. It maintains an hashmap instead, with all of the commands launched in the current session, and a count of how many times each command has been run. There is a Bash builtin meant to be used to interact with that hashmap: hash .

Example:

$ hash
hits   command

  1   /sbin/dmesg

  1   /usr/bin/host

  1   /bin/ls

  1   /usr/bin/top

  1   /usr/local/bin/docker

If you want to reset a single voice in the table you can run

$ hash dmesg #resets counter for dmesg

Use case 1: Reset messed up ENV

$ hash -r #clears the hashmap

When you use sandboxed environments like python’s VirtualEnv, you may end up in situations where you are running executables outside of the sandbox because PATH have been updated but the command is still in the hasmap, you can solve by clearing the hashmap

Use case 2: Executing command outside of PATH

$ hash -p filename name

You can even use hash to run executables not in the currant PATH without the need to put in in the path (useful if you don’t want to put the whole folder containing a lot of other executables).

bash shell builtin hash linux
Giuseppe Virzì

Giuseppe Virzì

Sicily, the island of sun Website Github LinkedIn

I'm a dreamer, but my feet are firmly bound to the ground. I like to face new challenges and exploit new solutions to old problems. I like everything allows me to work with new technologies. I guess I can say, in four words, that I like to learn.

Let's Encrypt meets GitLab

Moving our DC into Raspberry Pis

2020 Scaleout Ninja
Menu
  • Blog