Earlier quoted context omitted.
I feel like I'm going to be bit when I try commands interactively and then that act differently in a script. I think the current solution is the pragmatic one, don't standardize things you can't pull off standardizing! #!
The ls command does this: if output is to a tty then it prints in columns. Many commands add color when the output is a tty. I don’t think it would be a big issue.
Debian's Which Hunt
231–240 of 257 posts
Re: Debian's Which Hunt
#232Earlier quoted context omitted.
> "but it's non-standard!" can be fixed by making it a standard. But which which do you make standard? When one says it's non-standard, it normally means that you don't know if the way you are using it will work on other places.
Unix predates POSIX -> All Unix flavors were non standard + your reasoning -> POSIX cannot exist.
Standards exist on the real world, you can't just define them into existence without doing the work.
Re: Debian's Which Hunt
#233Earlier quoted context omitted.
tcsh on my system: $ tcsh --version tcsh 6.21.00 (Astron) 2019-05-08 (x86_64-apple-darwin) options wide,nls,dl,bye,al,kan,sm,rh,color,filec $ tcsh % command -v command command % command -V command command is a shell builtin % builtins | grep command %
Yeah, this is probably because macOS has done something very, very weird, and added an executable `/usr/bin/command` to the system. These are the contents on the old MacBook I have for work right here: #!/bin/sh # $FreeBSD: src/usr.bin/alias/generic.sh,v 1.2 2005/10/24 22:32:19 cperciva Exp $ # This file is in the public domain. builtin `echo %{0##*/} | tr \[:upper:] \[:lower]` ${1+"$@"} If you have SIP disabled, try…
[edit]
Not hard to remount root as writable
$ sudo mount -uw /
Password:
$ sudo mv /usr/bin/command /usr/bin/command.save
$ tcsh
% command -v command
command: Command not found.
% command -V command
command: Command not found.
%
Now reversing the changes % exit
exit
$ sudo mv /usr/bin/command.save /usr/bin/command
$ sudo mount -ur /
mount_apfs: volume could not be mounted: Invalid argument
mount: / failed with 66
Looks like I'll have to reboot to get the read-only state back.Re: Debian's Which Hunt
#234Better to follow standards anyway.
Re: Debian's Which Hunt
#235Re: Debian's Which Hunt
#236Earlier quoted context omitted.
You should be using ShellCheck or an equivalent linter on your shell scripts in POSIX mode if you want them to be portable; in POSIX mode it warns when using "which" instead of "command -v", and I think it also warns when using "whence" or "where" outside of zsh. There's a host of other Bashisms/GNUisms and other noncompliant antipatterns it checks for too. It's also worth reading through the POSIX manpages for POSIX…
> It's also worth reading through the POSIX manpages for POSIX sh if you want to be a good shell scripter. Or you can give the middle finger to POSIX, write shell scripts for bash, or even zsh, and end up with faster, more correct, and more beautiful shell scripts that in practice are just as portable as the ones that mindlessly follow the limited and bizarre POSIX shell spec. I'll write POSIXly-correct shell scripts…
Ok, pleas someone tell me how to use that "upvote" festure on HN :)
More on the subject, nor type nor command, nor constantly mentioned shellcheck recipe for all posixs actually do what which do - just returns path ! Looks like with some options, maybe, but it could be simply added with "which -h" - (standard *nix human readable form toggle), if -h is not already taken.
POSIX in the first place should just standarise which instead of inventing some "command".
Advice: to write best POSIX scripts write moustly-POSIX scripts and be done. Simply becose you should write big shell scripts in the first place. It is Perl job and it do it very better. And if you want more you need C and actually using system calls yourself, eg. for precise path not-globbing.
PS. Now just wait for Mr Poettering take on the subject becouse why be bothered by decades old working admins opinions or work conditions ? Or usable standards, or already working code, or not containers use cases, or just short, human friendly commandline ? Or not tangled and small codebases ?? Let's leave it to, I don't know, IBM ? Is systemd portable to AIX yet, any plans ? ;) WINDOWIZACION95 FTW
Re: Debian's Which Hunt
#237Earlier quoted context omitted.
There is no man or info page for "command" on Ubuntu, I guess it's just a bash builtin ("which command" doesn't find it). "which" has a man page though.
In bash, such things are documented in the help system: $ help command command: command [-pVv] command [arg ...] Execute a simple command or display information about commands. Runs COMMAND with ARGS suppressing shell function lookup, or display information about the specified COMMANDs. Can be used to invoke commands on disk when a function with the same name exists. Options: -p use a default value for PATH that is g…
Or just cranked-up a search engine.
So thanks for that.
Re: Debian's Which Hunt
#238Earlier quoted context omitted.
No, macOS has an executable called `command` so that it is available even on shells that don't implement the builtin
If you look at the last part of my comment, you’ll see it’s just a shell script that invokes the built-in.
It's a weird choice, because it means that command will sometimes tell you about 'builtins' that are not actually built in to the shell you invoke it from, including `command` itself.
Re: Debian's Which Hunt
#239Regardless of whether 'command -v' is a better alternative, 'which' is ensconced as standard practice in many shell scripts, and I would go so far as to say I think the majority of them that actually have to choose between the two use 'which'. Given that, having it print out a deprecation warning without considering what that means to all the people that use it is irresponsible. I help manage a few hundred servers. W…
Virtually every command can fail in some way, and they will all write to stderr (or worse, stdout, but that's not what happened here). How is that unexpected at all? How else would you communicate the change? (For the sake of argument, let's say the change is going to happen regardless, since you're arguing for the irresponsibleness of showing a warning.)
Re: Debian's Which Hunt
#240Here's how zsh 5.8 behaves on macOS: $ which {which,type,command,vi} which: shell built-in command type: shell built-in command command: shell built-in command /usr/bin/vi $ type {which,type,command,vi} which is a shell builtin type is a shell builtin command is a shell builtin vi is /usr/bin/vi $ command -v {which,type,command,vi} which type command /usr/bin/vi Which is quite different from how bash 5.1 behaves: bas…
"command -v" isn't reliable because several shells (including Dash) don't obey the POSIX standard. See https://github.com/oilshell/oil/blob/8fbc09bb3254cee944b0450...
Either way, it doesn't have that behavior like that for me on Debian: it behaves the same as bash. This is in dash:
$ command -v whoami
/usr/bin/whoami
$ echo $?
0
$ command -v echo
echo
$ echo $?
0
$ command -v nonexistent
$ echo $?
127
So that is with both a proper command (whoami), a shell built-in (echo), and neither (nonexistent). It's all as I would expect from a shell.Bash does exactly the same, although dash chooses 127 as exit status and bash chooses 1 but they're both nonzero (thus error statuses).