[Toybox] command -v vs type
Chet Ramey
chet.ramey at case.edu
Wed Aug 5 14:34:14 UTC 2026
On 8/4/26 2:50 PM, Rob Landley wrote:
> I was confused by command and type behaving _different_ when the help just
> says that -v is similar to "type".
Sure, it's similar in the sense that it describes how the shell will
interpret a command word.
> command: command [-pVv] command [arg ...]
> Execute a simple command or display information about commands.
> ...
> Exit Status:
> Returns exit status of COMMAND, or failure if COMMAND is not found.
>
> I thought maybe that meant the FIRST one listed was controlling the exit
> status, hence the "nothing cat" ordering above.
There are three ways to do it: any failed means failure, any succeeded
means success, and the status of the last command word controls the status.
There are shells that do each of them.
> But honestly, there are
> just SO many little weird...
>
> $ mkdir sub sub2
> $ tee echo hello sub/hello > sub2/hello
What's the `tee' supposed to do here?
> $ chmod +x sub2/hello
> $ PATH=sub:sub2:$PATH hello
> hello
Yes, it skips over non-executables...
> $ rm sub2/hello
> $ PATH=sub:sub2:$PATH hello
> bash: sub/hello: Permission denied
But will find and fail to execute a non-executable if it's the only one
with the name found using a $PATH search. This is for Bourne sh
compatibility. Most shells behave this way (yash is a notable exception).
> Then there's:
>
> $ PATH=sub:$PATH
> $ type sub/xx
> sub/xx is sub/xx
> $ type xx
> xx is sub/xx
> $ command -V sub/xx
> sub/xx is sub/xx
> $ command -V xx
> xx is /home/landley/toybox/toybox/sub/xx
>
> Why does that one uniquely expand to absolute path? (Not a _cannonical_
> path, if sub is a symlink it's still there...)
The first and third aren't going to be found using $PATH, so we can ignore
them.
The second is `type' reporting how it will be treated after a $PATH search.
Since `type' isn't POSIX, bash can use any format it chooses.
The last is a POSIX requirement:
"Executable utilities, regular built-in utilities, and any implementation-
provided functions that are found using the PATH variable (as described in
2.9.1.4 Command Search and Execution), shall be identified as such and
include the absolute pathname in the string."
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet at case.edu http://tiswww.cwru.edu/~chet/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature.asc
Type: application/pgp-signature
Size: 203 bytes
Desc: OpenPGP digital signature
URL: <http://lists.landley.net/pipermail/toybox/attachments/20260805/48015f93/attachment.sig>
More information about the Toybox
mailing list