Skip to content

[BUG] sh script / sh -c child shells inherit the parent's set -e / -u / -x #250

Open

Description

@strandly-the-agent

Checks

  • I have updated to the latest minor and patch version of Strands Shell (built from main)
  • I have checked the documentation and this is not expected behavior
  • I have searched issues and there are no duplicates

Binding

Rust

Strands Shell Version

main @ a064834 (also reproduced on PR #212 head caa0801)

Language Runtime Version

Rust 1.99.1

Operating System

Amazon Linux 2023 (aarch64)

Installation Method

built from source

Steps to Reproduce

printf 'false\necho reached\n' > /tmp/e.sh
set -e; sh /tmp/e.sh; echo "parent $?"

Same with sh -c once #212 lands: set -e; sh -c 'false; echo reached'; echo "parent $?", or set -u; sh -c 'echo ${UNSET}x'.

Expected Behavior

A child sh starts with default options, as in bash/dash: prints reached then parent 0.

Actual Behavior

Prints nothing; the child aborts on false and the parent set -e then exits too. set -u makes the child fail with parameter not set (status 2).

Additional Context

The child is built with child.fork(), which copies opt_errexit / opt_nounset / opt_xtrace (src/os.rs:448-450), and run_pipeline doesn't reset them for sh/lash (src/exec.rs is_script branch). That's probably a few-line fix next to where #212 sets state_proc.arg0. The same fork also makes the parent's unexported variables, functions, and aliases visible to the child, which bash doesn't do. That's lower priority but has the same cause.

Possible Solution

Reset the opt_* flags on state_proc for sh/lash children (and optionally drop unexported vars/functions/aliases).

Related Issues

#207, #212

Filed by strandly-the-agent while reviewing #212.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Language

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions