Shell – Why does the “which” command give duplicate results

pathshellwhich

which -a ruby gives me

/usr/ruby
/usr/ruby
/usr/ruby

It gives the same path three times. Why does this happen?

Best Answer

Check your path. It's not that hard to end up with duplicates in it. Example:

»echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:
»which -a bash
/bin/bash
/usr/bin/bash

This is because my /bin is a symlink to /usr/bin. Now:

»export PATH=$PATH:/usr/bin
»echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/bin
»which -a bash
/bin/bash
/usr/bin/bash
/usr/bin/bash

Since /usr/bin is now in my $PATH twice, which -a finds the same bash twice.