A cross-platform Go implementation of the which(1)
command, usable both as a CLI and library.
Usage of which:
-a List all instances of executables found (instead of just the first).
-s No output, just return 0 if all executables are found, or 1 if some were not found.
-v Print the version
Unlike the UNIX which(1)
command, even if multiple programs are given as input, only the first one found will be returned.
Chances are you don't really need this, since most UNIX-like OSes come with the more established (and significantly smaller) C implementation of which(1)
, either as a standalone binary, or as a shell builtin.
But if there's some reason this may be useful to you, you can use this just like the normal which(1)
:
$ which zsh
/usr/local/bin/zsh
$ which -a zsh
/usr/local/bin/zsh
/bin/zsh
$ which zsh bash sh
/usr/local/bin/zsh
$ which -a zsh bash sh
/usr/local/bin/zsh
/bin/zsh
/bin/bash
/bin/sh
$ if (which -s zsh bash); then
> echo 'I have zsh and bash installed';
> fi
I have zsh and bash installed
$ if (which -s zsh bash ash); then echo 'yup'
> else
> echo "I'm missing one of them...";
> fi
I'm missing one of them...
If you're writing a program in the Go language, it can be useful to not have to shell out to which(1)
to locate a binary.
The simplest usage is:
package main
import (
"fmt"
"github.com/hairyhenderson/go-which"
)
func main() {
zshPath := which.Which("zsh")
fmt.Printf("zsh found at %s", zshPath)
}
See the godocs for more information.
Copyright (c) 2018-2020 Dave Henderson