Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Simplest way to choose one of several commands



2009/9/7 Baptiste Daroussin <baptiste.daroussin@xxxxxxxxx>:
> 2009/9/7 Nikolai Weibull <now@xxxxxxxx>:
>> Is there a simpler way of doing the following?
>>
>> alias ls=${${(f)"$(whence -p {g,}ls)"}[1]}' --color'
>>
>> I need to select gls, if thatâs whatâs installed, otherwise ls.  (The
>> code above assumes that at least ls is installed, which isnât a
>> problem.)
> all the command in path are in the associative array $commands so
> assuming that at least ls is in the path :
> alias ls="${commands[gls]:-$commands[ls]} --color"
> it seems simpler to me

Some meandering through the man page later:
% echo ${${:-$commands[(R)*/(ls|cat)]}[1]}
/bin/ls
% echo ${${:-$commands[(R)*/(ls|cat)]}[2]}
/bin/cat

Side note, I would have expected $commands[(K)(ls|cat)] to list
/bin/ls and /bin/cat, but it doesn't (it's empty). $commands[(K)ls]
does give /bin/ls though.

-- 
Mikael Magnusson



Messages sorted by: Reverse Date, Date, Thread, Author