George Jor George Jor - 5 days ago 4x
Bash Question

function to detect cli installation

I have inline checking to detect the installation of cli packages to save time on installing existing package, but I found it is tedious and not that readable for those long list.

For example:

which -s redis-cli || brew install redis
which -s java || brew cask install java
which -s yarn || npm install -g yarn

Are there any function to make it nice looking? For example:

function npmInstall(name) {
if (which -s name) {

npm install -g name;

Thanks a lot!


You may pass client packages as parameters.


for cli in $@; do
  which "$cli" || npm install -g "$cli"

invoked with ./ redis java yarn