Linux and hash command


Welcome to the next pikoTutorial!

What is hash?

The hash command in Linux is a built-in shell utility used to optimize the execution of commands by recording paths of the invoked commands, so subsequent executions don’t need to search through the PATH variable again. You can see it in action by opening a new terminal window and calling:

hash

If that’s your first command in the terminal, you’ll see the following output:

hash: hash table empty

That’s because every hash table exists within a single shell session. Let’s then call a couple of commands:

python3 --version
git status
cd $HOME

Now when you call hash command, the output will show (almost) all commands that you have just used:

hits    command
   1    /usr/bin/python3
   1    /usr/bin/git

Almost because hash does not register calls of shell built-in command and that’s why you don’t see here cd command. If you want to clear the entire table you can do this by calling:

hash -r

Or, if you want to remove just one command (e.g. git), call:

hash -d git

Practical applications

Adding custom temporary commands

Sometimes there’s a need to test some executables in a convenient way, without putting them into any of the system directories or polluting the system’s PATH. This may happen e.g. when you want to test new versions of already installed tools or when you have a set of custom executables. This can be done easily with the hash command and -p option to manually assign a custom path to a command.

hash -p /path/to/executable command_name

From now on, you can invoke command_name in the terminal and it will execute your executable file from the given path. However, be aware that this is temporary and limited to the current shell session. If you open another terminal window, you won’t find command_name there.

Simplifying command calls in restricted environments

In secure or restricted environments, like corporate systems or controlled servers, users often have limited permissions to modify system configurations, including the PATH variable. By using the hash command together with -p option, users can directly point to required executables without changing system-wide settings, which is particularly beneficial in environments with stringent security protocols. This allows user to execute the desired tool without violating security policies or triggering monitoring systems, maintaining a streamlined workflow within a controlled setting.

Creating statistics of command usage

As you can see above, hash command counts the calls of individual commands. This can be used to monitor usage and create statistics, which can be especially valuable when commands/executables are called not by humans, but by a script. This allows to better understand what dependencies they rely on the most, what may lead to further optimizations..

Troubleshooting command execution issues

hash command is very useful in terms of debugging. If you have a certain command which at the first glance should work, but it doesn’t, hash command allows you to immediately take a look on what is the actual path of the executable you’re calling and thanks to this you can very quickly assess if the command is calling what you think it is calling.