Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Last active February 15, 2024 09:29
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save miguelmota/ed4ec562b8cd1781e7b20151b37de8a0 to your computer and use it in GitHub Desktop.
Save miguelmota/ed4ec562b8cd1781e7b20151b37de8a0 to your computer and use it in GitHub Desktop.
Golang check if command exists
package main
import (
"log"
"os/exec"
)
func main() {
path, err := exec.LookPath("ls")
if err != nil {
log.Fatal(err)
}
log.Println(path) // bin/ls
}
// as util
func commandExists(cmd string) bool {
_, err := exec.LookPath(cmd)
return err == nil
}
@cuzzea
Copy link

cuzzea commented Aug 28, 2019

thanks

@elie-g
Copy link

elie-g commented Nov 1, 2019

I haven't tried it but I guess this does not check for aliases too. Is there a way to do so?

@mohkale
Copy link

mohkale commented Sep 7, 2020

@DrunkenPoney aliases are a shell construct. They aren't passed to sub processes. You can create executable scripts with the name of your aliases and they will be discover able like this.

@thesobercoder
Copy link

@miguelmota Thanks!

@northbright
Copy link

Thanks!

@yangyang5214
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment