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:
hashIf that’s your first command in the terminal, you’ll see the following output:
hash: hash table emptyThat’s because every hash table exists within a single shell session. Let’s then call a couple of commands:
python3 --version
git status
cd $HOMENow 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/gitAlmost 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 -rOr, if you want to remove just one command (e.g. git), call:
hash -d gitPractical 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_nameFrom 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.
Read also:
- Sharing variable between bash scripts
- A 40-line LLM-based bash command executor in Python
- GTest and short-circuit evaluation in C++
- AI is powerful. Snippets are instant.
- From AUTOSAR to S-Core: the first C++ pub/sub implementation
- How to write Arduino Uno code with Python?
- Combining Bazel with Docker
- Running commands with timeout on Linux
- Running Python unit tests with CMake
- Thirdparty dependencies with FetchContent
- Bug of the week #11
- Combining CMake with Docker
- How to search the internet from Linux terminal?
- Folding expressions in C++
- How to derive from an enum in Python?
- Bug of the week #10
- Trying ROS2: client/server within a single container
- Make C++ a better place #4: Go as an alternative
- How to convert hex to dec in Linux terminal?
- Setting up a Python project with CMake
- Separating builds for different configs with Bazel
- Trying ROS2: pub/sub within a single container
- Bug of the week #9
- UDP multicasting with Python
- Destruction order vs thread safety in C++
- Let’s review some code: C++ #2
- Make C++ a better place #3: D as an alternative
- Registering callback using std::function in C++
- Bug of the week #8
- TCP client/server with Python
- Simple menus in Bash scripts with select
- Calling member function on a nullptr in C++
- Bug of the week #7
- Python lru_cache explained
- How to dockerize a Python application?
- Make C++ a better place #2: CppFront as an alternative
- Parameters combinations in GoogleTest
- Data transfer with curl
- Python reduce explained
- Bug of the week #6
- Custom literals in C++
- Linux and hash command
- 5 Python good practices which make life easier
- Let’s review some code: Python #1
- Make C++ a better place #1: What does better mean
- Enums vs enum class in C++
- Bug of the week #5
- UDP client/server with Python
- Hard links in Linux
- Functions calling order in unit tests in C++
- Bug of the week #4
- Yield in Python – state machines, coroutines and more
- Copy files from another branch with Git
- Make C++ a better place #0: Introduction
- 5 misconceptions about std::move in C++
- How to use xargs on Linux?
- How to test method call order with unittest in Python?
- Bug of the week #3
- Build & run C++ unit tests with CMake
- Arrange text with sort on Linux









