Skip to content

ideas around scripts with rubyshell in shebang #75

Description

@jjb

Intro

Did you know that recent versions of gem have an npx-style way of invoking a gem's script of the same name? So today, this already works 🎉

#!/usr/bin/env gem exec rubyshell exec

sh do
  puts date
  puts pwd
end
# $ chmod +x my_script
# $ ./my_script

Sun Jun  7 10:44:22 PDT 2026
/Users/john/tmp

Idea 1

You can add that to the docs

Idea 2

a "no-sh-block" mode where the entire file is assumed to be rubyshell, no sh do needed. Could implement this with a flag (rubyshell exec --top-level) or simply autodetect when the script runs (check for presence of sh do in the script, either with a simple grep or string search, or maybe fancier way with prism?).

Plot-twist: I lied a little

the actual shebang I used is: #!/usr/bin/env -S RUBYOPT=-W0 gem exec --silent rubyshell exec

RUBYOPT=-W0 and --silent are needed to keep the output nice

maybe that approach is fine or maybe the gem exec feature could be refined, I'm not sure what folks would consider safe as default behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions