golang/go · error

bad -count=: must be at least 1

Error message

bad -count=: must be at least 1

What it means

Thrown by the match function when the -count=N flag value parses successfully as an integer but is less than 1. The match function requires at least one expected match; zero or negative counts are meaningless and rejected after the Atoi parse succeeds.

Source

Thrown at src/cmd/internal/script/cmds.go:658

		},
		func(s *State, args ...string) (WaitFunc, error) {
			return nil, match(s, args, "", "grep")
		})
}

const matchUsage = "[-count=N] [-q] 'pattern'"

// match implements the Grep, Stdout, and Stderr commands.
func match(s *State, args []string, text, name string) error {
	n := 0
	if len(args) >= 1 && strings.HasPrefix(args[0], "-count=") {
		var err error
		n, err = strconv.Atoi(args[0][len("-count="):])
		if err != nil {
			return fmt.Errorf("bad -count=: %v", err)
		}
		if n < 1 {
			return fmt.Errorf("bad -count=: must be at least 1")
		}
		args = args[1:]
	}
	quiet := false
	if len(args) >= 1 && args[0] == "-q" {
		quiet = true
		args = args[1:]
	}

	isGrep := name == "grep"

	wantArgs := 1
	if isGrep {
		wantArgs = 2
	}
	if len(args) != wantArgs {
		return ErrUsage
	}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Use -count=1 or higher to assert an exact positive match count.
  2. To assert a pattern does NOT appear, simply omit -count and let the default no-match behavior apply (the pattern should not match).
  3. Check for sign errors if a negative value was unexpected.

Example fix

// Before (broken): stdout -count=0 'pattern'   // trying to assert no match
// After (fixed):  ! stdout 'pattern'                   // negate to assert absence
Defensive patterns

Strategy: validation

Validate before calling

// Validate -count is at least 1:
func validateCountMin(arg string) error {
    if !strings.HasPrefix(arg, "-count=") { return nil }
    n, _ := strconv.Atoi(arg[len("-count="):])
    if n < 1 {
        return fmt.Errorf("-count must be >= 1; to assert no match, negate the command with !")
    }
    return nil
}

Prevention

When it happens

Trigger: args[0] is like "-count=0" or "-count=-5" — strconv.Atoi succeeds returning a value < 1, triggering the `n < 1` check.

Common situations: Using -count=0 to assert 'no matches', which is incorrect — omit the -count flag and rely on the pattern not matching instead. Or a sign error / off-by-one producing a negative count.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/fb838f23c6d3f5ec. Report an issue: GitHub.