10

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?

6
  • why would you want to? Apr 18, 2017 at 16:47
  • 1
    @WalrustheCat the macOS version of less is from 2013, specifically version 458. The current Homebrew version is 487. Some enhancements have been made and bugs have been fixed between versions. Apr 18, 2017 at 19:57
  • @ ssdecontrol exactly. @WalrustheCat why I would want to doesn't really matter, or does it? However, if you think replacing system less might have undesired side effect, please share your thoughts.
    – stack_lech
    Apr 19, 2017 at 9:04
  • @Cpt_Jauchefuerst just curious mang Apr 19, 2017 at 16:18
  • This question / answer would also apply to other programs like e.g. nano
    – stack_lech
    Apr 28, 2017 at 12:00

3 Answers 3

15

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.

3
  • Worked like a charm. $hash -d less resulted in -bash: hash: less: not found but $hash -r did the job.
    – stack_lech
    Apr 19, 2017 at 9:29
  • What to do if you use zsh which is now the default? Feb 21, 2023 at 15:09
  • 2
    @CorneliusRoemer hash -r or rehash according to stackoverflow.com/a/36547391/2072269
    – muru
    Feb 21, 2023 at 15:48
4

If homebrew is correctly installed /usr/local/bin/ is checked before /usr/bin/. Check this with echo $PATH, this will show you the path's the OS is trying to find the command you entered and it is trying them in the order that is shown.

A little fancier
using the bash shell:

echo $PATH | awk '{ n = split($0, paths, ":"); for (i=0; ++i <= n;) print i, paths[i] }'

(using the fish shell):

echo $PATH | awk '{ n = split($0, paths, " "); for (i=0; ++i <= n;) print i, paths[i] }'

This will show an numbered list of the path's tried. Mine shows

1 /Users/peter/.gem/ruby/2.0.0/bin
2 /usr/local/sbin
3 /usr/local/bin
4 /usr/bin
5 /bin
6 /usr/sbin
7 /sbin
8 /Library/TeX/texbin

The system will traverse the directories in the above order looking for commands, in your case less. If it's not in the first one, it will try the next one until it runs out of options and throw up an error.

So if your echo $PATH shows similar results as mine you are using the homebrew version of less.

3
  • 7
    In the Unix spirit, small tools that do one job and do it well- echo $PATH | tr ':' '\n' | nl
    – fd0
    Apr 18, 2017 at 10:10
  • 2
    That's much more elegant. I didn't know about nl, and as they say when you have a hammer, everything looks like a nail.
    – PeterVP
    Apr 18, 2017 at 14:15
  • /usr/local/bin is indeed checked before /usr/bin but the /usr/bin/less was in the bash's hash table so I had to $hash -r.
    – stack_lech
    Apr 19, 2017 at 9:32
0

I restarted the shell, and this fixed the problem. Maybe a caching issue.

If this doesn't help, make sure that /opt/homebrew/bin appeares above /usr/bin when running

echo $PATH | tr ':' '\n' | nl`

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .