add functions, positional parameters, case and read

- functions: name() { ...; }, function name { ...; }, return, local,
  unset -f; in pipes, & and $(...) they run in a subshell that gets all
  function definitions and the positional parameters
- positional parameters $0, $1 ... ${10}, $#, "$@", "$*", $$; set by
  gosh script args, gosh -c command name args, function calls,
  source file args and set --; shift; for x without "in"
- case word in pattern|pattern) ...;; esac and { list; } groups
- read [-r] [-p prompt] [name ...] with $IFS splitting and $REPLY

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Florian WaltherandClaude Opus 5.5 committed 2026-10-04 17:20:59 +02:00
1 parent 9fe8b06fba
commit 443367f66f
9 files changed
+1629 -98

No files matched your search

+23 -12
View File
@@ -2,7 +2,8 @@ package main
// Command substitution: $(command) is replaced by the output of command,
// which runs in a subshell (a new gosh process), so that cd, set etc. in it
// do not change the shell.
// do not change the shell. Functions also run in a subshell in pipelines and
// in the background.
import (
"bytes"
@@ -67,24 +68,34 @@ func substEnd(s string, i int) int {
return -1
}
// subshellCommand returns a command that runs script in a new gosh process
// with the same $0, the given positional parameters and the functions of the
// shell. (Variables need not be passed: they are all in the environment.)
func subshellCommand(script string, params []string) (*exec.Cmd, error) {
path := subshellPath
if path == "" {
var err error
if path, err = os.Executable(); err != nil {
return nil, err
}
}
args := append([]string{"-c", functionDefs() + script, scriptName}, params...)
return exec.Command(path, args...), nil
}
// commandSubst runs command in a subshell and returns its output without
// trailing newlines. stdin and stderr are those of the shell.
func commandSubst(command string) (string, error) {
if strings.HasPrefix(command, "(") {
return "", errors.New("arithmetic expansion $((...)) is not supported")
}
path := subshellPath
if path == "" {
var err error
if path, err = os.Executable(); err != nil {
return "", err
}
}
var out bytes.Buffer
cmd := exec.Command(path, "-c", command)
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, &out, os.Stderr
err := cmd.Run()
cmd, err := subshellCommand(command, positional)
if err != nil {
return "", err
}
cmd.Stdin, cmd.Stdout, cmd.Stderr = stdio(0), &out, stdio(2)
err = cmd.Run()
substStatus = exitStatus(err)
if substStatus == 128+int(syscall.SIGINT) {
// Ctrl+C aborts the whole command, like in bash