4

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?

  • why would you want to? – Walrus the Cat Apr 18 '17 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. – shadowtalker Apr 18 '17 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. – Cpt_Jauchefuerst Apr 19 '17 at 9:04
  • @Cpt_Jauchefuerst just curious mang – Walrus the Cat Apr 19 '17 at 16:18
  • This question / answer would also apply to other programs like e.g. nano – Cpt_Jauchefuerst Apr 28 '17 at 12:00
12

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.

  • Worked like a charm. $hash -d less resulted in -bash: hash: less: not found but $hash -r did the job. – Cpt_Jauchefuerst Apr 19 '17 at 9:29
3

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.

  • 6
    In the Unix spirit, small tools that do one job and do it well- echo $PATH | tr ':' '\n' | nl – fd0 Apr 18 '17 at 10:10
  • 1
    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 '17 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. – Cpt_Jauchefuerst Apr 19 '17 at 9:32

You must log in to answer this question.

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