4 ms·
The form you propose runs `command`, which may have undesired side-effects. I always thought `which` to be standard, but TIL `command` (sh builtin) is[0]. [0]:
by mbivert 2y ago
The form you propose runs `command`, which may have undesired side-effects. I always thought `which` to be standard, but TIL `command` (sh builtin) is[0].
[0]: https://hynek.me/til/which-not-posix/ https://hynek.me/til/which-not-posix/
- stephenr 2y agoTo be clear: both alternatives shown below will invoke the same thing (`command -v gtimeout`). if [ -x "$(command -v gtimeout)" ]; then and if command -v gtimeout >/dev/null; then The first invokes it in a sub shell (and captures the output), the second invokes it directly and discards the output, using the return status of `command` as the input to `if`. The superficial reason the second is "preferred" is that it's slightly better performance wise. Not a huge difference, but it is a difference. However the hidden, and probably more impactful reason it's preferred, is that the first can give a false negative. If the thing you want to test before calling is implemented as a shell builtin, it will fail, because the `-x` mode of `test` (and thus `[`) is a file test, whereas the return value of `command -v` is whether or not the command can be invoked.
- mbivert 2y agoAh! I misread the parent, if thought he meant `if command` to look for a random command (e.g. `if grep`)