Version: v0.0.0-...-a69649c Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2014 License: Apache-2.0, GPL-2.0 Imports: 11 Imported by: 0



Build Status Go Walker

for the better grow of this package. some change may influence the old users, the old-code has tag: v0.1

install: go get

I like os/exec, so this golang package go-sh, is very like os/exec. But it really have some better experience than os/exec.

There are some features, listed bellow.

  • keep the variable environment (like export)
  • alias support (like alias in shell)
  • remember current dir
  • pipe command
  • shell build-in command test

Example is always important. I will show you how to use it.

sh: echo hello
go: sh.Command("echo", "hello").Run()

sh: export BUILD_ID=123
go: s = sh.NewSession().SetEnv("BUILD_ID", "123")

sh: alias ll='ls -l'
go: s = sh.NewSession().Alias('ll', 'ls', '-l')

sh: (cd /; pwd)
go: sh.Command("pwd", sh.Dir("/")).Run()

sh: test -d data || mkdir data
go: if ! sh.Test("dir", "data") { sh.Command("mkdir", "data").Run() }

sh: echo hello world | awk '{print $1}'
go: sh.Command("echo", "hello", "world").Command("awk", "{print $1}").Run()

sh: msg=$(echo hi)
go: msg, err := sh.Command("echo", "hi").Output()

If you need to keep env and dir, it is better to create a session

session := sh.NewSession()
session.SetEnv("BUILD_ID", "123")
# then call cmd
session.Command("echo", "hello").Run()
# set ShowCMD to true for easily debug
session.ShowCmd = true

for more information, it better to see docs. Go Walker


If you love this project, star it which will encourage the coder. pull requests are welcomed, if you want to add some new fetures.

support the author: alipay


this project is based on thanks for the author.

the reason to use golang shell

So what is go-sh. Sometimes we need to write some shell scripts, but shell scripts is not good at cross platform, but golang is good at that. Is there a good way to use golang to write scripts like shell? Use go-sh we can do it now.



Package go-sh is intented to make shell call with golang more easily. Some usage is more similar to os/exec, eg: Run(), Output(), Command(name, args...)

But with these similar function, pipe is added in and this package also got shell-session support.

Why I love golang so much, because the usage of golang is simple, but the power is unlimited. I want to make this pakcage got the sample style like golang.

// just like os/exec
sh.Command("echo", "hello").Run()

// support pipe
sh.Command("echo", "hello").Command("wc", "-c").Run()

// create a session to store dir and env

// shell buildin command - "test"
sh.Test("dir", "mydir")

// like shell call: (cd /; pwd)
sh.Command("pwd", sh.Dir("/")) same with sh.Command(sh.Dir("/"), "pwd")

// output to json and xml easily
v := map[string] int {}
err = sh.Command("echo", `{"nunber": 1}`).UnmarshalJSON(&v)




This section is empty.


This section is empty.


func Test

func Test(exp string, arg string) bool

expression can be dir, file, link

if sh.Test("dir", "mydir") {
	fmt.Println("mydir exists")


type Dir

type Dir string

type Session

type Session struct {
	Env     map[string]string
	Stdout  io.Writer
	Stderr  io.Writer
	ShowCMD bool // enable for debug
	// contains filtered or unexported fields

func Command

func Command(name string, a ...interface{}) *Session
out, err := sh.Command("echo", "hello").Output()
fmt.Println(string(out), err)

func NewSession

func NewSession() *Session

func (*Session) Alias

func (s *Session) Alias(alias, cmd string, args ...string)

func (*Session) Call

func (s *Session) Call(name string, a ...interface{}) error

combine Command and Run

func (*Session) Command

func (s *Session) Command(name string, a ...interface{}) *Session

func (*Session) Output

func (s *Session) Output() (out []byte, err error)

func (*Session) Run

func (s *Session) Run() (err error)

func (*Session) SetDir

func (s *Session) SetDir(dir string) *Session

func (*Session) SetEnv

func (s *Session) SetEnv(key, value string) *Session

func (*Session) Start

func (s *Session) Start() (err error)

start command

func (*Session) Test

func (s *Session) Test(expression string, argument string) bool

expression can be dir, file, link

func (*Session) UnmarshalJSON

func (s *Session) UnmarshalJSON(data interface{}) (err error)

unmarshal shell output to decode json

func (*Session) UnmarshalXML

func (s *Session) UnmarshalXML(data interface{}) (err error)

unmarshal command output into xml

func (*Session) Wait

func (s *Session) Wait() (err error)

Should be call after Start() only catch the last command error


Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL