Shell – What does ‘//’ mean in return from `which`

pathshellslashwhich

I have an executable script test under the full path /home/sawa/foo/bar/test. The directory /home/sawa/foo/bar is within $PATH, and has priority over the default ones including /usr/bin. When I do

`which test`

to see whether this command is correctly recognized, it returns

/home/sawa/foo/bar//test

with the double slash //. I know that there is a built in command with the same name test, and when I remove mine, this one under /usr/bin/test is returned by which, so I think it's interfering in some way.

  1. What does this double slash mean here, and why is it appearing here?
  2. My executable test does not seem to work correctly. Why is that?

Best Answer

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.

Related Question