Skip to content

Apply the options on a script's #! line - #9

Merged
hex2f merged 1 commit into
hex2f:mainfrom
ExpectedArgument:fix-shebang-options
Nov 6, 2026
Merged

hex2f merged 1 commit into
hex2f:mainfrom
ExpectedArgument:fix-shebang-options

Conversation

@ExpectedArgument

@ExpectedArgument ExpectedArgument commented Nov 6, 2026 •

Copy link
Copy Markdown

Fixes #3.

classify_file recognised a shell interpreter on the #! line but dropped what followed it, so #!/bin/bash -e, -u, -x and env -S bash -e had no effect on scripts run in-process, and the spawned path (GASH_NO_INPROC) didn't pass them on either.

  • The options are kept on Resolved (opts) and applied to the child shell with the command-line parser, so they behave exactly as on the command line. parse_cli gains a report flag for a quiet mode, and the set/shopt part of real_main moves into apply_options, which both now use.
  • As on Linux, the rest of the line is one argument: -e, -eu, +e and -x work, and -e -u is an error, as with bash. After env, with or without -S, it is split into words.
  • If the options can't be applied quietly (an invalid option, --version, ...), the script runs as a separate gash process, which reports it the way bash does, on the script's own stdout and stderr.
  • The spawned path passes the options before the script path.

Tests

  • New compat script 071_shebang_options.sh, with expected output from Git Bash 5.3.15 via regen_expected.ps1. It covers -e, -u, -eu, env -S bash -e, #!/bin/sh -e, +e, set +e after -e, -o pipefail as one argument (an error in bash), -u with arguments, and -x. It fails with v0.1.0 and passes with this branch.
  • cargo test passes: unit tests, 66/66 compat scripts and msys_tmp.

classify_file recognised a shell interpreter on the #! line but dropped
what followed it, so #!/bin/bash -e, -u, -x and env -S bash -e had no
effect on scripts run in-process, and the spawned path didn't pass them
either. They are now kept on Resolved and applied to the child shell
with the command-line parser: as on Linux, the rest of the line is one
argument, split into words after env. parse_cli gains a quiet mode and
the set/shopt part of real_main moves into apply_options, which both
use. If bash would report something (an invalid option, --version), the
script runs as a separate gash process, which does.

Fixes hex2f#3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@hex2f hex2f left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, and for the rebase and the test rename. It fixes #3. I found nothing to change.

The rebased branch passes cargo test: all 66 compat scripts, including the new 071_shebang_options. Moving set/shopt handling into apply_options, so that #! options go through the same parser as the command line, is a nice way to get bash's behaviour for errors like -o pipefail given as one argument.

Looks good to merge.

@hex2f
hex2f merged commit d72a125 into hex2f:main Nov 6, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Options on a script's shebang line are ignored (#!/bin/bash -e does not stop on errors)

2 participants