"which" broken under both FreeBSD and Debian

I have a shell script file named test.bash

If I want to obtain the path of this file, and I want to type
> which ./test.bash

The problem is:
1. with a GNU "which" it will return a path name, for example /home/user/test.bash

2. but under Debian linux it will return
./test.bash

3. same thing happened in FreeBSD again
./test.bash

Thus can I say this "which" command is both broken in FreeBSD and Debian?

Thx.
 
Thanks!

But I just want to get the full path of the file in a shell script. If so, I have to include it into my path each time. However, with a GNU compiled which, I don't have to worry about that. It will just return the full path, no matter if it in the path.

The "which" of FreeBSD and Debian act the same way, but in some other GNU linux, they can return the full path. Not just "./"+the_shell_file_name.

Why does that happen?

Beastie said:
How is this broken? Put test.bash inside your path ("." isn't), e.g. in your $HOME/bin and which(1) will return absolute paths, e.g. /home/user/bin/test.bash.
 
none_8200 said:
But I just want to get the full path of the file in a shell script. If so, I have to include it into my path each time. However, with a GNU compiled which, I don't have to worry about that. It will just return the full path, no matter if it in the path.

I'd suggest using realpath(1) to find the fully qualified path for an object instead of the which command.
 
fronclynne has it right, there is nothing in the Single Unix Specification detailing which. realpath is there, but only as a function call, not a shell command. The portable way to do this would be to use something like dirname(1):

Code:
path_to_script=`dirname $0`

Depending on what you are trying to do, this may be sufficient. Do you really need an absolute path or is a relative path okay?
 
gordon@ said:
fronclynne has it right, there is nothing in the Single Unix Specification detailing which. realpath is there, but only as a function call, not a shell command. The portable way to do this would be to use something like dirname(1):

Code:
path_to_script=`dirname $0`

Depending on what you are trying to do, this may be sufficient. Do you really need an absolute path or is a relative path okay?

I need the absolute path. `dirname $0` returns a "dot", if the file is the in current directory. But I can use `pwd` combo `basename` as a workaround. Thanks.

p.s. Does it really matter using a function call or a shell command? I can both use realpath and dirname in sh.
 
fronclynne said:
Dunno, not familiar with posix, but does posix say one way or the other*?

FYI, which is a tcsh(1) builtin, though /usr/bin/which does behave as you say.


*because if there's not an external standard, then no, you cannot reasonably say that one way of doing it is "broken"

Edit:
Well, you can look here (ugh, frames) but it doesn't seem to mention "which":
http://www.opengroup.org/onlinepubs/9699919799/

You are right. There is no such a topic in POSIX.

http://www.opengroup.org/onlinepubs/9699919799/idx/iw.html
http://www.opengroup.org/onlinepubs/9699919799/utilities/

Thanks.
 
none_8200 said:
I need the absolute path. `dirname $0` returns a "dot", if the file is the in current directory. But I can use `pwd` combo `basename` as a workaround. Thanks.

p.s. Does it really matter using a function call or a shell command? I can both use realpath and dirname in sh.

Depends on your goals. If you want a portable shell script, realpath may not work on all platforms.
 
Back
Top