weird resolving of path to command

0

I have the terminal editor 'nano' installed in two places on my mac

/usr/bin/nano

/opt/local/bin/nano

The installations are of different versions. The one in /usr does not support my configuration in ~/.nanorc and the one in /opt does.

When I open a file with the command 'nano file', errors are displayed, indicating that the one in /usr was used, however, if I run 'which nano'; the one in /opt shows up.

Isn't `which' meant to search the path for the default? And why wouldn't a call to 'nano' resolve to the same path?

I made a work-around by adding the following line to ~/.profile

alias nano='/opt/local/bin/nano'

Eldamir

Posted 2012-11-04T10:21:03.630

Reputation: 225

The real question is, why do you have two executable files with the same name in directories both of which are in your PATH variable? Remove or rename one of the nano files to nano.bak, and if you still need the binary in both locations, create a symbolic link to the remaining binary. – Breakthrough – 2012-11-04T14:12:15.983

Answers

1

The binaries are hashed and resolved in the order their parent directories appear in the PATH environment variable. Adjust it according to your needs. This is true for my experiences with bash, at least. Consider looking at the man page for your shell and look for COMMAND EXECUTION and PATH if what I'm telling you does not apply to your shell.

For example, if I wanted foo to mean /home/me/bin/foo and not /usr/local/bin/foo then I would put /home/me/bin/ before /usr/local/bin in the path, where before means left of:

PATH=(...):/home/me/bin:(...):/usr/local/bin:(...)

The path is commonly set up in /etc/profile or in ~/.profile.

Ярослав Рахматуллин

Posted 2012-11-04T10:21:03.630

Reputation: 9 076

That is my understanding of it too. I am just wondering; why then does 'which' not display the right one? Isn't that utility meant to search PATH in order? – Eldamir – 2012-11-05T07:43:07.130

It should, and does. Have you tried hash -d nano? Have you looked at hash -l? This seems like one of those cases when one oversees something and thus gets unexpected results. I really don't think which works incorrectly on your system. What could be happening is that nano gets hashed to /usr/bin/nano before the one in opt is found. Apart from that I'm out of advice. – Ярослав Рахматуллин – 2012-11-05T09:55:35.497