Debian's which hunt
Debian's which hunt
Posted Oct 28, 2021 15:00 UTC (Thu) by dskoll (subscriber, #1630)Parent article: Debian's which hunt
Huh, I did not know about command -v
. I'd always used the type
shell built-in, though it's output is not designed for computer-parsing.
Posted Oct 28, 2021 15:50 UTC (Thu)
by ncm (guest, #165)
[Link] (25 responses)
It should be a pretty small effort to replace uses of "which" in packages that use it. After that, replacing "which" itself with a one-liner that says "use command -v" then "exit 1" is another small effort. Moving it out of the package it is in seems like pointless extra work.
Posted Oct 28, 2021 16:06 UTC (Thu)
by Taywee (guest, #154131)
[Link] (16 responses)
For the purposes of most scripting use, POSIX is specified by the IEEE Computer Society, so that's who decided it. I don't think it much matters, though, as much of Linux use isn't constrained by POSIX anyway. I wonder how many build scripts would break if POSIX command line argument parsing was strictly followed (which mandates all option arguments before all positional arguments). I don't think having
Posted Oct 28, 2021 16:17 UTC (Thu)
by abatters (✭ supporter ✭, #6932)
[Link] (15 responses)
# which ls
# command -v ls
Posted Oct 28, 2021 18:08 UTC (Thu)
by Taywee (guest, #154131)
[Link] (3 responses)
Posted Oct 28, 2021 18:44 UTC (Thu)
by joey (guest, #328)
[Link]
#!/bin/sh
Whether that will work with command -v will depend on what shell /bin/sh uses, since different shells read different dotfiles which could contain aliases. Yeah, I had a case of this in my ~/bin/.
Posted Oct 28, 2021 20:37 UTC (Thu)
by jond (subscriber, #37669)
[Link]
Posted Oct 29, 2021 10:27 UTC (Fri)
by GhePeU (subscriber, #56133)
[Link]
$ which fakeprogram; echo $?
$ command -v fakeprogram; echo $?
$ command -V fakeprogram; echo $?
Posted Oct 28, 2021 19:01 UTC (Thu)
by marcH (subscriber, #57642)
[Link] (10 responses)
$ which type
I stopped using "which" after that and switched to type -p
BTW https://github.com/koalaman/shellcheck/wiki/SC2230
(everyone should use shellcheck)
Posted Oct 29, 2021 6:24 UTC (Fri)
by weberm (guest, #131630)
[Link] (4 responses)
$ which type
Nothing scratches my back just like which does.
Posted Oct 29, 2021 13:59 UTC (Fri)
by pj (subscriber, #4506)
[Link] (2 responses)
IMO, `type` seems to be the best-thought-out version of this tool, and should be made standard.
Posted Oct 29, 2021 21:15 UTC (Fri)
by weberm (guest, #131630)
[Link] (1 responses)
$ type -a ls
IMO which being an external tool actually comes in quite handy here. It has its drawbacks, but also its benefits.
Posted Oct 29, 2021 21:29 UTC (Fri)
by marcH (subscriber, #57642)
[Link]
Posted Oct 29, 2021 16:28 UTC (Fri)
by overfl0w (guest, #155055)
[Link]
So when I execute the 'vim' command, actually 'nvim' is executed.
$ which vim
So in this particular case 'which' did not return the correct path. Perhaps this is due to the fact that 'vim' is a valid command. But if I executed 'vim', it would not execute the executable returned by 'which'. I found this by accident as I hadn't heard about 'command -v' before.
Posted Oct 29, 2021 14:40 UTC (Fri)
by smoogen (subscriber, #97)
[Link] (3 responses)
Posted Oct 29, 2021 15:26 UTC (Fri)
by marcH (subscriber, #57642)
[Link] (2 responses)
Ignoring functions , built-in and aliases is really harmful. This has cost me a lot of time so I simply stopped using it. Problem solved.
Posted Oct 29, 2021 15:52 UTC (Fri)
by marcH (subscriber, #57642)
[Link]
- You should use "which" if you're still stuck with a C-shell for some reason
More C-shell history below
- I'm not saying "which" must be removed or deprecated or whatever (I don't care). But I am saying it is harmful for bash and other Bourne shell users and that they should stop using it for their own good.
Posted Oct 29, 2021 21:38 UTC (Fri)
by buck (subscriber, #55985)
[Link]
and, yes, count me in as one of the folks who never knew about the existence of `command` and had always resorted to `type -p` (and will continue to use it for my [occasional] purposes, which is in scripts and not interactive shells, when having `command -v` tell me about an alias isn't gonna work)
as for the matter somebody else raised about fetishizing POSIX, one only has to look at a `configure` script (maybe more so in the past than nowadays, as i guess checks for things like ULTRIX quirks may have gone by the wayside) to find plenty of reasons to be thankful for when an OS or distribution tries to take its cue from a formal or de-facto or what's-everybody-else-doing standard to reduce the heterogeneity when it doesn't add any meaningful value, ... if it's not already too well entrenched. and Linux itself got to be the dominant force it is by using (might-as-well-be) POSIX conformance as the calling card to present to get the door open, for it then to barge in and trample all the other guests
Posted Nov 12, 2021 18:44 UTC (Fri)
by rmc032 (guest, #131846)
[Link]
Which ironically, `type -p` is a GNU specific option and isn't POSIX compliant, as far as I can tell. I think I have the correct version here, but skimming the POSIX shell standard, it seems that the specification for the `type` utility doesn't list any options:
Figured it was worth mentioning since a decent chunk of the article discussion is about how `command -v` is the POSIX way to find a command rather than `which`
Posted Oct 29, 2021 2:18 UTC (Fri)
by da4089 (subscriber, #1195)
[Link] (4 responses)
There's a kind of fundamentalism in the Unix world that sees the POSIX specification as some sort of authority. In my experience, this was mostly a problem in the transition to System V: Solaris tended towards POSIX compliance (or was it the other way around?) and SunOS was more historically based on BSD. The /usr/ucb tree saved my finger memory for many years.
HP/UX had a similar split, IIRC.
Linux has mostly been free of this kind of dogma. It took the GNU coreutils/shutils/etc and ran with them, but every so often little things popped up, and the gaps were plugged by things like bsdutils, bsdmainutils, and (apparently) debianutils.
Sometimes it's useful to have people move forward: newer ways can be better ways. But 'which' vs. 'command -v' ... is that really an improvement? Is it really a problem to have both available by default?
Posted Oct 29, 2021 3:39 UTC (Fri)
by unixbhaskar (guest, #44758)
[Link]
Posted Oct 29, 2021 8:11 UTC (Fri)
by mbunkus (subscriber, #87248)
[Link]
https://lists.debian.org/debian-ctte/2021/10/msg00001.html
Basically he simply didn't want to have to deal with that shit anymore. Which (no pun intended) I emphatically agree with.
Posted Nov 1, 2021 21:56 UTC (Mon)
by mirabilos (subscriber, #84359)
[Link]
Posted Nov 4, 2021 16:13 UTC (Thu)
by Karellen (subscriber, #67644)
[Link]
I think mostly it's just about reliability and portability. If you only use features that's been ratified by POSIX then your code will be maximally portable to all Unices, no matter if they're derived from SysV or BSD, or from neither (e.g. Linux). If you want your code to be as useful to as many people as possible (or to be sellable to as many people as possible) then sticking to POSIX guarantees that. And if your code doesn't work on a platform, you get to blame the OS vendor, and to only grudgingly write a work-around (and maybe only if you get paid enough). On the vendor side, if you want your platform to get any traction then you want as much existing code as possible to work on it, which means you need to embrace POSIX. Yeah, you can try and value-add stuff on top of that and go for the "extend" step (which traditional Unices did), but that carries a bad taste for Free Software/Linux devs (I wonder why?) and software devs don't always like to take advantage because then their software might not be usable by all their current users/customers. Of the complaints I've seen about systemd and wayland (among others), the ones I have the most sympathy with are those from software devs who write apps that are meant to run on Linux and the BSDs, and for whom moving to the new native Linux APIs would mean either increasing their workload or dropping support for some platforms. Yes, back-compat support for init scripts and X11 exists and is supported, but some will still end up feeling like 2nd class citizens.
Posted Oct 29, 2021 5:42 UTC (Fri)
by nilsmeyer (guest, #122604)
[Link]
Also first time I heard it, and glad I did because now I can once more considered to be competent in the eyes of Clint Adams.
I'm now wondering how much of what I use in my day to day shell use are builtins vs. dedicated programs (I'm using zsh). `which` in zsh is at least explicit about what's builtin, e.g.:
Posted Oct 29, 2021 9:04 UTC (Fri)
by tchernobog (guest, #73595)
[Link]
Posted Nov 2, 2021 20:41 UTC (Tue)
by SomeOtherGuy (guest, #151918)
[Link]
I'd never heard of command -v, there's even a bug with some bashes like mine where command -v returns the first match, not an actual executionable match.
It's going to be difficult
Posted Oct 28, 2021 17:48 UTC (Thu)
by oever (guest, #987)
[Link] (1 responses)
That seems at least as good as "command -v $command".
Posted Oct 28, 2021 18:18 UTC (Thu)
by smcv (subscriber, #53363)
[Link]
In bash, yes. In other shells or on non-Linux OSs, no, so this is less portable than `command -v` (and in particular dash doesn't implement the -P or -p options).
POSIX specifies the `type` command, but does not specify any valid options for it.
Posted Oct 29, 2021 9:46 UTC (Fri)
by MarcB (guest, #101804)
[Link]
$ man command
It is only mentioned in "builtins".
Also, it is not a drop-in replacement for which:
Debian's which hunt
Debian's which hunt
which
could easily just be aliased to command -v
, or replaced with a small wrapper script around it, given that the exit status is what the average scripter would depend upon anyway.which
as some universally-available alias for command -v
would do any harm.Debian's which hunt
/usr/bin/ls
alias ls='ls --color=auto'
Debian's which hunt
If you need to find whether an executable exists somewhere in PATH even if a built-in, function, or alias would subvert it, then the GNU which behavior is going to be more useful, but I can't think of a lot of use-cases for that, honestly.
Debian's which hunt
foo=$(which foo)
PATH=something:else
$foo
Debian's which hunt
Debian's which hunt
1
alias fakeprogram='/usr/bin/notarealprogram -a'
0
fakeprogram is aliased to `/usr/bin/notarealprogram -a'
0
Debian's which hunt
/usr/bin/type
$ type type
type is a shell builtin
Debian's which hunt
$ type type
type is a shell builtin
$ type -p type
$
# yes, its exit status tells me type exists, but not where
$ type ls
ls is aliased to `ls --color=auto'
# I hate that alias with a passion but sometimes forget to remove it. Not the point. It ought to tell me where the expanded alias is pulling that 'ls' in.
$ which ls
/bin/ls
$ command -v ls
alias ls='ls --color=auto'
$ command -V ls
ls is aliased to `ls --color=auto'
Debian's which hunt
ls is aliased to `ls -F --color=auto'
ls is /bin/ls
Debian's which hunt
mksh: whence: -a: unknown option
Debian's which hunt
Debian's which hunt
alias vim='nvim' in .bashrc
/usr/bin/vim
$ command -v vim
alias vim='nvim'
So /usr/bin/type is a shell script which just does:
Debian's which hunt
#!/usr/bin/sh
builtin type "$@"
It is there so you can run type foo in CSH or some other shell and still have it work. So which is telling you what could be executed via your path no matter what your shell is.
Debian's which hunt
Debian's which hunt
$ tcsh
> which which
which: shell built-in command.
Debian's which hunt
Debian's which hunt
- OpenGroup link: https://pubs.opengroup.org/onlinepubs/9699919799/utilitie...
- Linux POSIX man page link: https://man7.org/linux/man-pages/man1/type.1p.html
Debian's which hunt
Debian's which hunt
Debian's which hunt
Debian's which hunt
Debian's which hunt
There's a kind of fundamentalism in the Unix world that sees the POSIX specification as some sort of authority.
Debian's which hunt
zsh:~ $ command -v command
command
zsh:~ $ which command
command: shell built-in command
Debian's which hunt
Debian's which hunt
Debian's which hunt
Debian's which hunt
Debian's which hunt
No manual entry for command
$ which ls
/bin/ls
$ command -v ls
alias ls='ls --color=auto'