can1357/oh-my-pi · error · ToolError

Invalid bash env name: ${key}

Error message

Invalid bash env name: ${key}

What it means

normalizeBashEnv validates each key of the tool call's env parameter against BASH_ENV_NAME_PATTERN before applying it to the bash process. Any key containing characters outside the allowed identifier set (POSIX-style env names like MY_VAR) throws this ToolError naming the offending key, so an invalid variable never reaches the shell.

Source

Thrown at packages/coding-agent/src/tools/bash.ts:391

	  };

interface ManagedBashJobHandle {
	jobId: string;
	completion: Promise<ManagedBashJobCompletion>;
	getLatestText: () => string;
	stopUpdates: () => void;
}

function normalizeResultOutput(result: BashResult | BashInteractiveResult): string {
	return result.output || "";
}

function normalizeBashEnv(env: Record<string, string> | undefined): Record<string, string> | undefined {
	if (!env || Object.keys(env).length === 0) return undefined;
	const normalized: Record<string, string> = {};
	for (const [key, value] of Object.entries(env)) {
		if (!BASH_ENV_NAME_PATTERN.test(key)) {
			throw new ToolError(`Invalid bash env name: ${key}`);
		}
		normalized[key] = value;
	}
	return normalized;
}

function escapeBashEnvValueForDisplay(value: unknown): string {
	return String(value)
		.replaceAll("\\", "\\\\")
		.replaceAll("\n", "\\n")
		.replaceAll("\r", "\\r")
		.replaceAll("\t", "\\t")
		.replaceAll('"', '\\"')
		.replaceAll("$", "\\$")
		.replaceAll("`", "\\`");
}

function formatBashEnvAssignments(env: Record<string, unknown> | undefined): string {

View on GitHub (pinned to 9690622007)

Solutions

  1. Rename the key to a valid shell identifier: letters, digits, underscores only, not starting with a digit (e.g. MY_VAR).
  2. Remove any '=value' from the key — values belong in the value field, not the key.
  3. Strip shell syntax; pass plain names like FOO, not 'export FOO' or 'FOO=1'.
  4. Set unconventional variables inside the command itself if the name cannot be made pattern-valid.

Example fix

// before
await bash.run(cmd, { env: { "MY-VAR": "1" } });
// after
await bash.run(cmd, { env: { MY_VAR: "1" } });
Defensive patterns

Strategy: validation

Validate before calling

const BASH_ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
for (const k of Object.keys(env ?? {})) if (!BASH_ENV_NAME.test(k)) throw new Error(`Invalid bash env name: ${k}`);

Type guard

const isValidEnvName = (k: string): boolean => /^[A-Za-z_][A-Za-z0-9_]*$/.test(k);

Try / catch

try { await bash.run(cmd, { env }); } catch (e) { if (e instanceof ToolError && e.message.startsWith('Invalid bash env name:')) { /* sanitize the named key and retry */ } else throw e; }

Prevention

When it happens

Trigger: A tool call supplies env with an invalid name — e.g. 'MY-VAR' (hyphen), '1PATH' (leading digit), 'FOO BAR' (space), 'export FOO' (accidentally passing a shell statement), or an empty key — so BASH_ENV_NAME_PATTERN.test(key) fails.

Common situations: LLM-generated tool arguments including shell syntax in env keys; copying 'KEY=value' pairs (with '=value') into env keys; intended lowercase-with-dashes names that are invalid for POSIX env vars.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/68c8e2934a399a56. Report an issue: GitHub.