Around Go: command line · 3 MIN READ

Bash passes arguments; quoting defines their boundaries

Read a small build script without splitting paths or accidentally invoking a shell through Go.

Bash is useful for connecting build and deployment tools on a developer machine. Go itself does not require Bash, Docker or Kubernetes. Learning their boundaries helps when a Go program becomes part of a service workflow; these commands are reading examples, not app terminal commands.

01

Preserve a path as one argument

Unquoted parameter expansion can perform word splitting and filename expansion. Quote a variable when its value should be one argument. In Bash, an array preserves several arguments without rebuilding a shell command string.

bash
#!/usr/bin/env bash
set -u
project_dir="${1:-.}"
args=(-C "$project_dir" test ./...)
go "${args[@]}"
Read the code

With a directory containing spaces, the array still supplies one directory argument. The script uses Bash-specific array syntax; it is not a portable /bin/sh script. The -C option changes Go's working directory before running the command.

02

Keep data out of shell syntax

When a Go process starts a tool, exec.Command passes an argument vector directly. This avoids asking a shell to reinterpret semicolons, substitutions or spaces. It does not by itself make the target command safe; validate its allowed operation and arguments.

go
cmd := exec.CommandContext(ctx, "go", "test", "./...")
cmd.Dir = projectDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
    return fmt.Errorf("go test: %w", err)
}
Read the code

This is native Go code for a development tool. It does not add subprocess execution to the iOS sandbox. Avoid sh -c with concatenated user input.

03

Choose Go when the script stops being glue

A few fixed tool invocations can remain a script. Structured retries, complex state, concurrent work and testable domain rules often belong in Go. Quote "$@" when forwarding original arguments, avoid eval, and handle the exit status at the point where failure changes the workflow.

TAKE THIS WITH YOU

Preserve argument boundaries. Use Bash for small orchestration and direct process argument vectors from native Go when no shell language is needed.

Check your understanding

Which statement is correct about quoted bash array expansion?

Go to the source

Progress stays in this browser. No account needed.