Homebrew vs System Less – How to Use Homebrew Less Instead of System Less

homebrew

I've installed less via

homebrew install less

but less starts the system version of less, not the one installed via homebrew. which less shows:

$ which -a less
/usr/local/bin/less
/usr/bin/less

where /usr/local/bin/less is a symlink to ../Cellar/less/487/bin/less (homebrew less)
and /usr/bin/less is the system less. echo $LESS returns nothing. How can I start homebrew less instead of system less by default?

Best Answer

This might be due to bash's hash table of commands in PATH still using the old value. Remove the hash of less, or clear the hash, so that bash re-searches PATH:

hash -d less
# or, hash -r

You can also manually add a path to the table:

hash -p /usr/local/bin/less less

From the Bash manual (emphasis mine):

If the name is neither a shell function nor a builtin, and contains no slashes, Bash searches each element of $PATH for a directory containing an executable file by that name. Bash uses a hash table to remember the full pathnames of executable files to avoid multiple PATH searches (see the description of hash in Bourne Shell Builtins). A full search of the directories in $PATH is performed only if the command is not found in the hash table.