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

Open
opened 2026-02-16 18:08:35 -05:00 by yindo · 1 comment
Owner

Originally created by @taxilian on GitHub (Jan 28, 2026).

Originally assigned to: @thdxr on GitHub.

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.

Originally created by @taxilian on GitHub (Jan 28, 2026). Originally assigned to: @thdxr on GitHub. ### Feature hasn't been suggested before. - [x] 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 ```typescript "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.
yindo added the discussion label 2026-02-16 18:08:35 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Jan 28, 2026):

This issue might be a duplicate of existing issues. Please check:

  • #9292: [FEATURE]: Expose session context to child processes via environment variables - requests exposing OPENCODE_SESSION_ID and session metadata via env vars
  • #6936: [FEATURE]: Pass agents bash env variables OR pass agents bashrc files - requests ability to pass environment variables or bashrc to bash commands
  • #10453: [FEATURE]: create and expose model-id environment variables for bash from CLI - requests exposing model information via environment variables

While these issues have different specific use cases, they all request the ability to set/inject environment variables for bash commands. Your proposal for an env parameter in the bash tool schema would be a general solution that could address all of these needs.

Feel free to ignore if your specific implementation is sufficiently different from these existing requests.

@github-actions[bot] commented on GitHub (Jan 28, 2026): This issue might be a duplicate of existing issues. Please check: - #9292: [FEATURE]: Expose session context to child processes via environment variables - requests exposing OPENCODE_SESSION_ID and session metadata via env vars - #6936: [FEATURE]: Pass agents bash env variables OR pass agents bashrc files - requests ability to pass environment variables or bashrc to bash commands - #10453: [FEATURE]: create and expose model-id environment variables for bash from CLI - requests exposing model information via environment variables While these issues have different specific use cases, they all request the ability to set/inject environment variables for bash commands. Your proposal for an `env` parameter in the bash tool schema would be a general solution that could address all of these needs. Feel free to ignore if your specific implementation is sufficiently different from these existing requests.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#7890