[Toybox] command -v vs type

Rob Landley rob at landley.net
Wed Aug 5 20:44:14 UTC 2026


On 8/5/26 09:34, Chet Ramey wrote:
 >> 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?

Sorry, cut and paste from a test and screwed up the fixing up, it was 
probably originally:

tee <<<'echo hello' sub/hello > sub2/hello

(Creates a small shell script in two places.)

>> $ 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).

I am learning all SORTS of new things. And then going "so do type -p and 
or type -P have the same behavior as exec" and I'm still trying to wrap 
my head around...

$ mkdir sub; touch sub/walrus # but NOT chmod +x
$ PATH=sub:$PATH type -P walrus
sub/walrus
$ PATH=sub:$PATH type -a walrus
bash: type: walrus: not found
$ PATH=sub:$PATH type -Pa walrus
$ echo $?
1
$ touch sub/xx; chmod +x sub/xx
$ PATH=sub:$PATH type -Pa xx walrus
sub/xx
$ echo $?
1

So... -P shows the nonexecutable file as a success, -a complains the 
nonexecutable file isn't found as a verbose failure, -Pa together say 
nothing but return 1, and when asked to evaluate two files it shows the 
executable one then returns silent error because the second wasn't 
found/executable.

And I need ALL those tests because each one of those behaviors is 
inconsistent with other parts of command/type.

>> 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.

Again, I was cutting and pasting a block of tests where I'm calling the 
corners (or perhaps calling all the corner cases).

> 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."

My confusion was that type _also_ found it via the $PATH but didn't 
expand that to abspath.

Anyway, it sounds like all this weirdness and inconsistency is 
intentional. Can't say I'm surprised. (Disappointed, but not in bash per 
se...)

Come up with tests, what bash does is what bash does, don't expect a 
rationale behind it other than "this shell from 1982" or "posix only 
says to do it for that"...

> Chet

Rob


More information about the Toybox mailing list