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

Re: Simplest way to choose one of several commands



On Mon, Sep 7, 2009 at 23:49, Mikael Magnusson<mikachu@xxxxxxxxx> wrote:

> 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

Ah, thanks.  I forgot about $commands.  I wish it was something
simpler, but perhaps Iâm asking for too much.

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

I can one-up you on that:

$commands[(r)*/(g|)ls]

Note, though, that neither mine nor your version chooses gls over ls
if both are installed.  This just chooses whatever happens to be
first, and, Iâm guessing, it will be pretty random.



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