Search notes:

Bash: remember the full path of previous called executables with set -h

set -h (which is set by default) specifies to remember (referred to as to hash) the path in which an executable is located when it was first called.
Thus, when an executable is called again later, bash doesn't have to search all directories in the $PATH environment variable, which should, at least theoretically, improve performance when executing a script.
To turn the hashing off, set +h is needed.
Hashing can also be turned on and off with set -o hashall and set +o hashall, respectively.
This behaviour is demonstrated with the following two scripts foo.sh and call-foo.sh.
call-foo.sh is executed with either ./call-foo.sh +h or ./call-foo.sh -h.

call-foo.sh

if [[ $# != 1 ]]; then
   echo "Specify -h or +h"
   exit 1
fi
if [[ $1 != -h && $1 != +h ]]; then
   echo "Specify -h or +h"
   exit 1
fi

set $1
mkdir -p dir

export PATH=dir:.:$PATH


foo.sh
type foo.sh

cp foo.sh dir

foo.sh
type foo.sh

rm dir/foo.sh
Github repository about-Bash, path: /built-in/set/h/call-foo.sh
When executed with -h, it prints
hello
foo.sh is hashed (./foo.sh)
hello
foo.sh is hashed (./foo.sh)
When executed with +h, it prints
hello
foo.sh is ./foo.sh
hello
foo.sh is dir/foo.sh

foo.sh

#!/bin/bash
echo hello
Github repository about-Bash, path: /built-in/set/h/foo.sh

See also

Bash builtins
set
Bash history

Index