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

advertisements

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

or

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

or

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