The best way to define & ldquo; Conditional & rdquo; Alias ​​in Shell


What's the best way to define an alias if, let's say, "ag" executable is found?

if (( $+commands[ag] )) ; then alias grep='ag'; fi


[[ -s $(which ag) ]] && alias grep='ag'


if $(which ag >& /dev/null); then alias grep='ag'; fi

or ...?

By best, I mean more robust, more performant and/or more portable (Bash, Zsh).

What's your advice?

I don't see how the simple obvious POSIX variant would have any drawbacks compared to the (mostly non-portable) alternatives you propose.

type ag >/dev/null 2>&1 && alias grep=ag