Skip to content

feat(bash): add env parameter for setting environment variables #11065

@taxilian

Description

@taxilian

Feature hasn't been suggested before.

  • I have verified this feature I'm about to request hasn't been suggested before.

Describe the enhancement you want to request

Issue Description

Problem

Plugins cannot inject environment variables into bash commands. There are a number of current feature requests which could be done with plugins if that was allowed, and it's a cheap / low risk fix.

Use Case

I want to build a plugin that sets AGENT_MODE=primary|secondary and AGENT_ID=<session_id> on all bash commands. This allows tracking whether commands run in the main agent or subagent sessions, as well as letting me keep track of some state in an external tool.

Currently, the only workaround is prepending VAR=value to commands, which is hacky and unreliable.

Proposed Solution

Add an optional env parameter to the bash tool that accepts a record of environment variables to set for the spawned process.

Example Usage

"tool.execute.before": async (input, output) => {
  if (input.tool !== "bash") return

  const session = await ctx.client.getSession(input.sessionID)
  const isSubagent = session?.parentID != null

  output.args.env = {
    AGENT_MODE: isSubagent ? "secondary" : "primary",
    AGENT_ID: input.sessionID,
  }
}

Implementation

  • Add env: z.record(z.string(), z.string()).optional() to bash tool schema
  • Merge env vars with process.env in spawn call
  • Add tests for single and multiple environment variables

This would also allow issues like #10247 to be done in a plugin, thus reducing the need for specialized niche things in opencode.

Metadata

Metadata

Assignees

Labels

discussionUsed for feature requests, proposals, ideas, etc. Open discussion

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions