on main help sign in
allow configuring shell (#157)
commite8b4bb0eddd1f54925821b4f7ffbee9dda3f905f authorEd Zynda <ezynda3@users.noreply.github.com> dateThu 15 May 2025, 16:53 (1 year ago) parent36faca4
3 files changed+61−3
README.md+22 internal/config/config.go+15 internal/llm/tools/shell/shell.go+24−3
README.md+22
@@ -96,6 +96,24 @@ You can configure OpenCode using environment variables:
9696| `AZURE_OPENAI_ENDPOINT` | For Azure OpenAI models |
9797| `AZURE_OPENAI_API_KEY` | For Azure OpenAI models (optional when using Entra ID) |
9898| `AZURE_OPENAI_API_VERSION` | For Azure OpenAI models |
99+| `SHELL` | Default shell to use (if not specified in config) |
100+
101+### Shell Configuration
102+
103+OpenCode allows you to configure the shell used by the bash tool. By default, it uses the shell specified in the `SHELL` environment variable, or falls back to `/bin/bash` if not set.
104+
105+You can override this in your configuration file:
106+
107+```json
108+{
109+ "shell": {
110+ "path": "/bin/zsh",
111+ "args": ["-l"]
112+ }
113+}
114+```
115+
116+This is useful if you want to use a different shell than your default system shell, or if you need to pass specific arguments to the shell.
99117
100118### Configuration File Structure
101119
@@ -136,6 +154,10 @@ You can configure OpenCode using environment variables:
136154 "maxTokens": 80
137155 }
138156 },
157+ "shell": {
158+ "path": "/bin/bash",
159+ "args": ["-l"]
160+ },
139161 "mcpServers": {
140162 "example": {
141163 "type": "stdio",
internal/config/config.go+15
@@ -73,6 +73,12 @@ type TUIConfig struct {
7373 Theme string `json:"theme,omitempty"`
7474}
7575
76+// ShellConfig defines the configuration for the shell used by the bash tool.
77+type ShellConfig struct {
78+ Path string `json:"path,omitempty"`
79+ Args []string `json:"args,omitempty"`
80+}
81+
7682// Config is the main configuration structure for the application.
7783type Config struct {
7884 Data Data `json:"data"`
@@ -85,6 +91,7 @@ type Config struct {
8591 DebugLSP bool `json:"debugLSP,omitempty"`
8692 ContextPaths []string `json:"contextPaths,omitempty"`
8793 TUI TUIConfig `json:"tui"`
94+ Shell ShellConfig `json:"shell,omitempty"`
8895 AutoCompact bool `json:"autoCompact,omitempty"`
8996}
9097
@@ -217,6 +224,14 @@ func setDefaults(debug bool) {
217224 viper.SetDefault("tui.theme", "opencode")
218225 viper.SetDefault("autoCompact", true)
219226
227+ // Set default shell from environment or fallback to /bin/bash
228+ shellPath := os.Getenv("SHELL")
229+ if shellPath == "" {
230+ shellPath = "/bin/bash"
231+ }
232+ viper.SetDefault("shell.path", shellPath)
233+ viper.SetDefault("shell.args", []string{"-l"})
234+
220235 if debug {
221236 viper.SetDefault("debug", true)
222237 viper.Set("log.level", "debug")
internal/llm/tools/shell/shell.go+24−3
@@ -11,6 +11,8 @@ import (
1111 "sync"
1212 "syscall"
1313 "time"
14+
15+ "github.com/opencode-ai/opencode/internal/config"
1416)
1517
1618type PersistentShell struct {
@@ -57,12 +59,31 @@ func GetPersistentShell(workingDir string) *PersistentShell {
5759}
5860
5961func newPersistentShell(cwd string) *PersistentShell {
60− shellPath := os.Getenv("SHELL")
62+ // Get shell configuration from config
63+ cfg := config.Get()
64+
65+ // Default to environment variable if config is not set or nil
66+ var shellPath string
67+ var shellArgs []string
68+
69+ if cfg != nil {
70+ shellPath = cfg.Shell.Path
71+ shellArgs = cfg.Shell.Args
72+ }
73+
6174 if shellPath == "" {
62− shellPath = "/bin/bash"
75+ shellPath = os.Getenv("SHELL")
76+ if shellPath == "" {
77+ shellPath = "/bin/bash"
78+ }
79+ }
80+
81+ // Default shell args
82+ if len(shellArgs) == 0 {
83+ shellArgs = []string{"-l"}
6384 }
6485
65− cmd := exec.Command(shellPath, "-l")
86+ cmd := exec.Command(shellPath, shellArgs...)
6687 cmd.Dir = cwd
6788
6889 stdinPipe, err := cmd.StdinPipe()