Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I evaluate a environment variable using Go's os/exec

Tags:

go

How do I get Go to evaluate the $PATH variable. I currently just prints "$PATH"

I have the following code

package main

import (
        "fmt"
        "log"
        "os/exec"
)

func main() {
        out, err := exec.Command("echo","$PATH").Output()
        if err != nil {
                log.Fatal(err)
        }
        fmt.Printf("%s\n",out)
}
like image 529
ppone Avatar asked Nov 21 '13 18:11

ppone


1 Answers

You need to use os.Getenv("PATH")

package main

import (
  "fmt"
  "log"
  "os/exec"
  "os"
)

func main() {
  out, err := exec.Command("echo",os.Getenv("PATH")).Output()
  if err != nil {
    log.Fatal(err)
  }
  fmt.Printf("%s\n",out)
}
like image 82
jefflab Avatar answered Oct 03 '22 15:10

jefflab