{"id":13511686,"url":"https://github.com/codeskyblue/go-sh","last_synced_at":"2025-12-15T17:20:57.057Z","repository":{"id":13910870,"uuid":"16609849","full_name":"codeskyblue/go-sh","owner":"codeskyblue","description":"like python-sh, for easy call shell with golang.","archived":false,"fork":false,"pushed_at":"2020-07-15T23:02:17.000Z","size":50,"stargazers_count":1119,"open_issues_count":10,"forks_count":139,"subscribers_count":41,"default_branch":"master","last_synced_at":"2025-04-12T01:54:10.131Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codeskyblue.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-02-07T09:23:39.000Z","updated_at":"2025-03-23T09:05:39.000Z","dependencies_parsed_at":"2022-07-10T05:00:32.775Z","dependency_job_id":null,"html_url":"https://github.com/codeskyblue/go-sh","commit_stats":null,"previous_names":["shxsun/go-sh"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeskyblue%2Fgo-sh","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeskyblue%2Fgo-sh/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeskyblue%2Fgo-sh/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeskyblue%2Fgo-sh/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codeskyblue","download_url":"https://codeload.github.com/codeskyblue/go-sh/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248505873,"owners_count":21115354,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T03:01:06.145Z","updated_at":"2025-12-15T17:20:56.754Z","avatar_url":"https://github.com/codeskyblue.png","language":"Go","readme":"## go-sh\n[![wercker status](https://app.wercker.com/status/009acbd4f00ccc6de7e2554e12a50d84/s \"wercker status\")](https://app.wercker.com/project/bykey/009acbd4f00ccc6de7e2554e12a50d84)\n[![Go Walker](http://gowalker.org/api/v1/badge)](http://gowalker.org/github.com/codeskyblue/go-sh)\n\n*If you depend on the old api, see tag: v.0.1*\n\ninstall: `go get github.com/codeskyblue/go-sh`\n\nPipe Example:\n\n\tpackage main\n\n\timport \"github.com/codeskyblue/go-sh\"\n\n\tfunc main() {\n\t\tsh.Command(\"echo\", \"hello\\tworld\").Command(\"cut\", \"-f2\").Run()\n\t}\n\nBecause I like os/exec, `go-sh` is very much modelled after it. However, `go-sh` provides a better experience.\n\nThese are some of its features:\n\n* keep the variable environment (e.g. export)\n* alias support (e.g. alias in shell)\n* remember current dir\n* pipe command\n* shell build-in commands echo \u0026 test\n* timeout support\n\nExamples are important:\n\n\tsh: echo hello\n\tgo: sh.Command(\"echo\", \"hello\").Run()\n\n\tsh: export BUILD_ID=123\n\tgo: s = sh.NewSession().SetEnv(\"BUILD_ID\", \"123\")\n\n\tsh: alias ll='ls -l'\n\tgo: s = sh.NewSession().Alias('ll', 'ls', '-l')\n\n\tsh: (cd /; pwd)\n\tgo: sh.Command(\"pwd\", sh.Dir(\"/\")).Run()\n\n\tsh: test -d data || mkdir data\n\tgo: if ! sh.Test(\"dir\", \"data\") { sh.Command(\"mkdir\", \"data\").Run() }\n\n\tsh: cat first second | awk '{print $1}'\n\tgo: sh.Command(\"cat\", \"first\", \"second\").Command(\"awk\", \"{print $1}\").Run()\n\n\tsh: count=$(echo \"one two three\" | wc -w)\n\tgo: count, err := sh.Echo(\"one two three\").Command(\"wc\", \"-w\").Output()\n\n\tsh(in ubuntu): timeout 1s sleep 3\n\tgo: c := sh.Command(\"sleep\", \"3\"); c.Start(); c.WaitTimeout(time.Second) # default SIGKILL\n\tgo: out, err := sh.Command(\"sleep\", \"3\").SetTimeout(time.Second).Output() # set session timeout and get output)\n\n\tsh: echo hello | cat\n\tgo: out, err := sh.Command(\"cat\").SetInput(\"hello\").Output()\n\n\tsh: cat # read from stdin\n\tgo: out, err := sh.Command(\"cat\").SetStdin(os.Stdin).Output()\n\n\tsh: ls -l \u003e /tmp/listing.txt # write stdout to file\n\tgo: err := sh.Command(\"ls\", \"-l\").WriteStdout(\"/tmp/listing.txt\")\n\nIf you need to keep env and dir, it is better to create a session\n\n\tsession := sh.NewSession()\n\tsession.SetEnv(\"BUILD_ID\", \"123\")\n\tsession.SetDir(\"/\")\n\t# then call cmd\n\tsession.Command(\"echo\", \"hello\").Run()\n\t# set ShowCMD to true for easily debug\n\tsession.ShowCMD = true\n\nBy default, pipeline returns error only if the last command exit with a non-zero status. However, you can also enable `pipefail` option like `bash`. In that case, pipeline returns error if any of the commands fail and for multiple failed commands, it returns the error of rightmost failed command.\n\n\tsession := sh.NewSession()\n\tsession.PipeFail = true\n\tsession.Command(\"cat\", \"unknown-file\").Command(\"echo\").Run()\n\nBy default, pipelines's std-error is set to last command's std-error. However, you can also combine std-errors of all commands into pipeline's std-error using `session.PipeStdErrors = true`.\n\nfor more information, it better to see docs.\n[![Go Walker](http://gowalker.org/api/v1/badge)](http://gowalker.org/github.com/codeskyblue/go-sh)\n\n### contribute\nIf you love this project, starring it will encourage the coder. Pull requests are welcome.\n\nsupport the author: [alipay](https://me.alipay.com/goskyblue)\n\n### thanks\nthis project is based on \u003chttp://github.com/codegangsta/inject\u003e. thanks for the author.\n\n# the reason to use Go shell\nSometimes we need to write shell scripts, but shell scripts are not good at working cross platform,  Go, on the other hand, is good at that. Is there a good way to use Go to write shell like scripts? Using go-sh we can do this now.\n","funding_links":[],"categories":["Go"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeskyblue%2Fgo-sh","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodeskyblue%2Fgo-sh","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeskyblue%2Fgo-sh/lists"}