I would guess that you have /home/sawa/foo/bar/
on your path - i.e. a path with a trailing slash.
which
is iterating over each element of $PATH
and appending /argv[1]
and checking for the existence of that file. That causes a double-slash - one from the $PATH
part, and one from /argv[1]
.
A double-slash is no problem. It is collapsed to a single slash by the kernel. Only at the beginning of a path may a double-slash have special meaning, and not always then.
As for test
not working, ensure you are not using the shell built-in when calling test
. You usually do this by using a full path, but with bash you can also use enable -n test
to disable the built-in test
command.
The three possibilities that come to mind for me:
- An alias exists for
emacs
(which you've checked)
- A function exists for
emacs
- The new
emacs
binary is not in your shell's PATH hashtable.
You can check if you have a function emacs
:
bash-3.2$ declare -F | fgrep emacs
declare -f emacs
And remove it:
unset -f emacs
Your shell also has a PATH hashtable which contains a reference to each binary in your PATH. If you add a new binary with the same name as an existing one elsewhere in your PATH, the shell needs to be informed by updating the hashtable:
hash -r
Additional explanation:
which
doesn't know about functions, as it is not a bash builtin:
bash-3.2$ emacs() { echo 'no emacs for you'; }
bash-3.2$ emacs
no emacs for you
bash-3.2$ which emacs
/usr/bin/emacs
bash-3.2$ `which emacs` --version | head -1
GNU Emacs 22.1.1
New binary hashtable behaviour is demonstrated by this script.
bash-3.2$ PATH=$HOME/bin:$PATH
bash-3.2$ cd $HOME/bin
bash-3.2$ cat nofile
cat: nofile: No such file or directory
bash-3.2$ echo echo hi > cat
bash-3.2$ chmod +x cat
bash-3.2$ cat nofile
cat: nofile: No such file or directory
bash-3.2$ hash -r
bash-3.2$ cat nofile
hi
bash-3.2$ rm cat
bash-3.2$ cat nofile
bash: /Users/mrb/bin/cat: No such file or directory
bash-3.2$ hash -r
bash-3.2$ cat nofile
cat: nofile: No such file or directory
Although I didn't call it, which cat
would always return the first cat
in my PATH, because it doesn't use the shell's hashtable.
Best Answer
Check your path. It's not that hard to end up with duplicates in it. Example:
This is because my /bin is a symlink to /usr/bin. Now:
Since /usr/bin is now in my $PATH twice,
which -a
finds the same bash twice.