DEV Community

abdfn
abdfn

Posted on

5 1

gosh - Run powershell and bash commands easly in go.

GitHub logo abdfnx / gosh

⌨ A golang library for executing bash & powershell commands easly.

Run powershell and bash commands easly in go.

Examples

Run one command on both shell and powershell



import "github.com/abdfnx/gosh"

// run a command
gosh.Run("git status")

// run a command with output
err, out, errout := gosh.RunOut("whoami")

if err != nil {
  log.Printf("error: %v\n", err)
  fmt.Print(errout)
}

fmt.Print(out)


Enter fullscreen mode Exit fullscreen mode

How gosh.Run("COMMAND") works ?



// `Run` executes the same command for shell and powershell
func Run(cmd string) {
    err, out, errout := ShellOutput("")

    if runtime.GOOS == "windows" {
        err, out, errout = PowershellOutput(cmd)
    } else {
        err, out, errout = ShellOutput(cmd)
    }

    if err != nil {
        log.Printf("error: %v\n", err)
        fmt.Print(errout)
    }

    fmt.Print(out)
}


Enter fullscreen mode Exit fullscreen mode

Powershell



import "github.com/abdfnx/gosh"

// run a command
gosh.PowershellCommand(`Write-Host "hello from powershell"`)

// run a script
gosh.PowershellCommand(`
  $git_username = git config user.name

  Write-Host $git_username
`)

// run a command with output
err, out, errout := gosh.PowerShelloutput(`[System.Environment]::SetEnvironmentVariable("Path", $Env:Path + ";$APP_PATH\bin", [System.EnvironmentVariableTarget]::User)`)

if err != nil {
  log.Printf("error: %v\n", err)
  fmt.Print(errout)
}

fmt.Print(out)


Enter fullscreen mode Exit fullscreen mode

Bash/Shell



import "github.com/abdfnx/gosh"

// run a command
gosh.ShellCommand(`echo "shell or bash?"`)

// run a script
gosh.ShellCommand(`
  mood="👨‍💻"

  if [ $mood != "😪" ]; then
    echo "still coding"
  fi
`)

// run a command with output
err, out, errout := gosh.Shelloutput(`curl --silent "https://api.github.com/repos/abdfnx/resto/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/'`)

if err != nil {
  log.Printf("error: %v\n", err)
  fmt.Print(errout)
}

fmt.Print(out)


Enter fullscreen mode Exit fullscreen mode

thank you for your time and don't forgot to star the repo if you like it

Image of PulumiUP 2025

Explore What’s Next in DevOps, IaC, and Security

Join us for demos, and learn trends, best practices, and lessons learned in Platform Engineering & DevOps, Cloud and IaC, and Security.

Save Your Spot

Top comments (0)

Image of PulumiUP 2025

From Cloud to Platforms: What Top Engineers Are Doing Differently

Hear insights from industry leaders about the current state and future of cloud and IaC, platform engineering, and security.

Save Your Spot

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay