krz/orgstar

A native macOS editor for org-mode files. editor org-mode swift

Commit 901f898203

901f898203aab908018eed0d4b257f234797e920

parent: 1a30f921e8

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-08 04:31 UTC

Run shell :stdin and :cmdline blocks as a script under a session

org-babel-sh-evaluate runs the external script whenever :stdin or :cmdline
is given, ignoring the session; :shebang alone still uses the session.

Layout: unified · split

Sources/OrgCore/Compute/Babel.swift +3 −2
@@ -411,10 +411,11 @@ public enum Babel {
411411 let lines = shellAssignments(vars, shell: executable, params: params)
412412 let cmdline = params.single[":cmdline"]
413413 let shebang = params.single[":shebang"].flatMap { $0.contains(where: { !$0.isWhitespace }) ? $0 : nil }
414 if session == nil, stdin != nil || cmdline != nil || shebang != nil {
414 if stdin != nil || cmdline != nil || (session == nil && shebang != nil) {
415415 // `org-babel-sh-evaluate`: an executable script, run by the shell with the
416416 // command line after it and `:stdin` as its input; the result is its standard
417 // output, or its exit status for `:results value`.
417 // output, or its exit status for `:results value`. `:stdin` and `:cmdline`
418 // run it even with a session, which the script doesn't use.
418419 var script = (shebang ?? "#!/usr/bin/env \(executable)") + "\n"
419420 if params.single[":padline"] != "no" { script += "\n" }
420421 script += (lines + [body]).joined(separator: "\n") + (valueIsExitStatus ? "\necho $?" : "")
Tests/OrgCoreTests/BabelTests.swift +6
@@ -156,6 +156,10 @@ struct BabelTests {
156156 "#+begin_src sh :results value :cmdline x\necho one\n#+end_src\n",
157157 "#+begin_src bash :shebang #!/bin/bash :cmdline \"a b\" c :results output\necho $#\n#+end_src\n",
158158 "#+begin_src sh :stdin src :results output\ntr a-z A-Z\n#+end_src\n\n#+NAME: src\n#+begin_src sh\necho hello\n#+end_src\n",
159 // `:stdin` and `:cmdline` run a script rather than the session.
160 "#+begin_src sh :session s1 :cmdline a b :results output\necho $# $1\n#+end_src\n",
161 "#+NAME: in\n| hello |\n\n#+begin_src sh :session s2 :stdin in :results output\ncat\n#+end_src\n",
162 "#+begin_src bash :session s3 :shebang #!/bin/bash :cmdline x :var v=3\necho $v $1 ${BASH_VERSION:+bash}\n#+end_src\n",
159163 // `:var` from other blocks' results.
160164 "#+begin_src sh :var x=five\necho $((x+1))\n#+end_src\n\n#+NAME: five\n#+begin_src sh\necho 5\n#+end_src\n",
161165 "#+begin_src python :var t=tbl\nreturn [len(t), t[1][0]]\n#+end_src\n\n#+NAME: tbl\n#+begin_src python\nreturn [[1, 2], [3, 4]]\n#+end_src\n",
@@ -218,6 +222,8 @@ struct BabelTests {
218222 (setq org-confirm-babel-evaluate nil
219223 org-babel-python-command "python3"
220224 default-directory temporary-file-directory)
225 ;; No shell session in batch; the cases with one don't use it.
226 (advice-add 'org-babel-sh-initiate-session :override (lambda (s &rest _) (unless (member s '(nil "none")) s)))
221227 (org-babel-execute-src-block))
222228 """))
223229 }