Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 51fd2d3

Browse files
authored
fix: Force bash for non-standard flags to exec (#226)
This addresses a regression introduced by #224. POSIX sh does not define arguments to the exec built-in, resulting in an error when using 'exec -a' to set the process name (argv[0]). This change executes /bin/bash instead of sh, and also changes to use 'exec -l', which automatically handles the hyphen prefix required to trigger login shell behavior. This also explicitly runs /bin/sh, aligning the behavior of "coder sh" and the frontend Terminal application.
1 parent c30c076 commit 51fd2d3

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

internal/cmd/shell.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,20 @@ coder sh front-end-dev cat ~/config.json`,
8383

8484
func shell(cmd *cobra.Command, cmdArgs []string) error {
8585
ctx := cmd.Context()
86-
command := "sh"
87-
args := []string{"-c"}
86+
var command string
87+
var args []string
8888
if len(cmdArgs) > 1 {
89+
command = "/bin/sh"
90+
args = []string{"-c"}
8991
args = append(args, strings.Join(cmdArgs[1:], " "))
9092
} else {
9193
// Bring user into shell if no command is specified.
9294
shell := "$(getent passwd $(id -u) | cut -d: -f 7)"
93-
name := "-$(basename " + shell + ")"
94-
args = append(args, fmt.Sprintf("exec -a %q %q", name, shell))
95+
96+
// force bash for the '-l' flag to the exec built-in
97+
command = "/bin/bash"
98+
args = []string{"-c"}
99+
args = append(args, fmt.Sprintf("exec -l %q", shell))
95100
}
96101

97102
envName := cmdArgs[0]

0 commit comments

Comments
 (0)