diff --git a/Library/Homebrew/dev-cmd/sh.rb b/Library/Homebrew/dev-cmd/sh.rb
index 5247cbea6625b5593a2ddf7e09520f3ee29df387..3cfb769484192a216332d761ea3ee66f751fca2c 100644
--- a/Library/Homebrew/dev-cmd/sh.rb
+++ b/Library/Homebrew/dev-cmd/sh.rb
@@ -10,18 +10,19 @@ module Homebrew
   def sh_args
     Homebrew::CLI::Parser.new do
       usage_banner <<~EOS
-        `sh` [<options>]
+        `sh` [<options>] [<file>]
 
-        Start a Homebrew build environment shell. Uses our years-battle-hardened
-        Homebrew build logic to help your `./configure && make && make install`
-        or even your `gem install` succeed. Especially handy if you run Homebrew
+        Homebrew build environment that uses years-battle-hardened
+        build logic to help your `./configure && make && make install`
+        and even your `gem install` succeed. Especially handy if you run Homebrew
         in an Xcode-only configuration since it adds tools like `make` to your `PATH`
         which build systems would not find otherwise.
       EOS
       flag   "--env=",
              description: "Use the standard `PATH` instead of superenv's when `std` is passed."
-
-      max_named 0
+      flag   "-c=", "--cmd=",
+             description: "Execute commands in a non-interactive shell."
+      max_named 1
     end
   end
 
@@ -39,21 +40,29 @@ module Homebrew
       # superenv stopped adding brew's bin but generally users will want it
       ENV["PATH"] = PATH.new(ENV["PATH"]).insert(1, HOMEBREW_PREFIX/"bin")
     end
-    subshell = if ENV["SHELL"].include?("zsh")
-      "PS1='brew %B%F{green}%~%f%b$ ' #{ENV["SHELL"]} -d"
+
+    ENV["VERBOSE"] = "1" if args.verbose?
+
+    if args.cmd.present?
+      safe_system(ENV["SHELL"], "-c", args.cmd)
+    elsif args.named.present?
+      safe_system(ENV["SHELL"], args.named.first)
     else
-      "PS1=\"brew \\[\\033[1;32m\\]\\w\\[\\033[0m\\]$ \" #{ENV["SHELL"]}"
+      subshell = if ENV["SHELL"].include?("zsh")
+        "PS1='brew %B%F{green}%~%f%b$ ' #{ENV["SHELL"]} -d"
+      else
+        "PS1=\"brew \\[\\033[1;32m\\]\\w\\[\\033[0m\\]$ \" #{ENV["SHELL"]}"
+      end
+      puts <<~EOS
+        Your shell has been configured to use Homebrew's build environment;
+        this should help you build stuff. Notably though, the system versions of
+        gem and pip will ignore our configuration and insist on using the
+        environment they were built under (mostly). Sadly, scons will also
+        ignore our configuration.
+        When done, type `exit`.
+      EOS
+      $stdout.flush
+      safe_system subshell
     end
-    ENV["VERBOSE"] = "1"
-    puts <<~EOS
-      Your shell has been configured to use Homebrew's build environment;
-      this should help you build stuff. Notably though, the system versions of
-      gem and pip will ignore our configuration and insist on using the
-      environment they were built under (mostly). Sadly, scons will also
-      ignore our configuration.
-      When done, type `exit`.
-    EOS
-    $stdout.flush
-    safe_system subshell
   end
 end
diff --git a/docs/Manpage.md b/docs/Manpage.md
index 2124276e6c534ada6535fe57b97ec196c4c7c693..0afe671ba49647876f24a30ded1c3e03cae34d10 100644
--- a/docs/Manpage.md
+++ b/docs/Manpage.md
@@ -1106,16 +1106,18 @@ Run a Ruby instance with Homebrew's libraries loaded, e.g.
 * `-e`:
   Execute the given text string as a script.
 
-### `sh` [*`options`*]
+### `sh` [*`options`*] [*`file`*]
 
-Start a Homebrew build environment shell. Uses our years-battle-hardened
-Homebrew build logic to help your `./configure && make && make install`
-or even your `gem install` succeed. Especially handy if you run Homebrew
+Homebrew build environment that uses years-battle-hardened
+build logic to help your `./configure && make && make install`
+and even your `gem install` succeed. Especially handy if you run Homebrew
 in an Xcode-only configuration since it adds tools like `make` to your `PATH`
 which build systems would not find otherwise.
 
 * `--env`:
   Use the standard `PATH` instead of superenv's when `std` is passed.
+* `-c`, `--cmd`:
+  Execute commands in a non-interactive shell.
 
 ### `sponsors`
 
diff --git a/manpages/brew.1 b/manpages/brew.1
index 98262471716c2e61b14d6643936da583823ecc6c..c53bfddef02386fcedc921e9daac8389e1a0bc5a 100644
--- a/manpages/brew.1
+++ b/manpages/brew.1
@@ -1522,13 +1522,17 @@ Load a library using \fBrequire\fR\.
 \fB\-e\fR
 Execute the given text string as a script\.
 .
-.SS "\fBsh\fR [\fIoptions\fR]"
-Start a Homebrew build environment shell\. Uses our years\-battle\-hardened Homebrew build logic to help your \fB\./configure && make && make install\fR or even your \fBgem install\fR succeed\. Especially handy if you run Homebrew in an Xcode\-only configuration since it adds tools like \fBmake\fR to your \fBPATH\fR which build systems would not find otherwise\.
+.SS "\fBsh\fR [\fIoptions\fR] [\fIfile\fR]"
+Homebrew build environment that uses years\-battle\-hardened build logic to help your \fB\./configure && make && make install\fR and even your \fBgem install\fR succeed\. Especially handy if you run Homebrew in an Xcode\-only configuration since it adds tools like \fBmake\fR to your \fBPATH\fR which build systems would not find otherwise\.
 .
 .TP
 \fB\-\-env\fR
 Use the standard \fBPATH\fR instead of superenv\'s when \fBstd\fR is passed\.
 .
+.TP
+\fB\-c\fR, \fB\-\-cmd\fR
+Execute commands in a non\-interactive shell\.
+.
 .SS "\fBsponsors\fR"
 Print a Markdown summary of Homebrew\'s GitHub Sponsors, suitable for pasting into a README\.
 .