I want to know how to check whether a command is installed.
Specifically, I want to alias vi
to vim
on all machines where vim
is a valid command. And I want to keep my *rc files generic. But some OSes don't come with vim
installed by default. And in others, vi
is Vi IMproved, yet vim
isn't a valid command.
I figured I could do this (bash style):
#If the version info from vi includes the phrase "Vi IMproved", then vi IS vim.
vimInfo=$(vi --version 2> /dev/null | head -n 1 | grep -Fi 'VI IMPROVED')
if [[ -z $vimInfo ]]; then
if [[ -f $(which vim) ]]; then
#if the value returned by 'which' is a valid directory, then vim is installed
alias vi='vim'
else
#vim is not installed
echo "Vi IMproved (vim) is not installed";
fi
fi
But is there a better way? And is the which
command guaranteed to be present on all unix-like machines?
P.S. I have *rc files for nearly every popular shell (bash, tcsh, zsh, ksh), and I intend to apply the solution in all of them. Therefore, the solution should not be shell-specific. The syntax can be, though.
type
doesn't work in tcsh. – A.B. Apr 20 '15 at 17:54