On 29/04/2024 19:22, Ihor Radchenko wrote:
Matt writes:

#+begin_src bash :cmdline "1 2 3"
[...]
To force quotes in the :cmdline one can do

#+begin_src bash :cmdline "\"1 2 3\""
echo "$1"
#+end_src

I consider it as a kind of pitfall inconsistent with DWIM concept. An idea of a kludge is below.

#+begin_src sh :cmdline "1 2 3" :results verbatim
  printf '%s\n' "$@"
#+end_src

#+RESULTS:
: 1 2 3

#+begin_src sh :cmdline 1 2 3 :results verbatim
  printf '%s\n' "$@"
#+end_src

#+RESULTS:
: 1
: 2
: 3

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 73fb70c26..efba97f2c 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -3363,7 +3363,7 @@ (defun org-babel-read (cell &optional inhibit-lisp-eval)
        ((save-match-data
(and (string-match "^[[:space:]]*\"\\(.*\\)\"[[:space:]]*$" cell)
                 (not (string-match "[^\\]\"" (match-string 1 cell)))))
-         (read cell))
+         (propertize (read cell) 'org-babel-value 'quoted-string))
        (t (org-no-properties cell))))

 (defun org-babel--string-to-number (string)
diff --git a/lisp/ob-shell.el b/lisp/ob-shell.el
index 35d9e9376..77c3fe261 100644
--- a/lisp/ob-shell.el
+++ b/lisp/ob-shell.el
@@ -300,6 +300,9 @@ (defun org-babel-sh-evaluate (session body &optional params stdin cmdline)
 of the statements in BODY, if RESULT-TYPE equals `value' then
 return the value of the last statement in BODY."
   (let* ((shebang (cdr (assq :shebang params)))
+         (cmdline-quote
+          (and cmdline
+               (get-text-property 0 'org-babel-value cmdline) "\""))
          (async (org-babel-comint-use-async params))
         (results-params (cdr (assq :result-params params)))
         (value-is-exit-status
@@ -329,7 +332,9 @@ (defun org-babel-sh-evaluate (session body &optional params stdin cmdline)
                         nil
                         (if shebang (when cmdline (list cmdline))
                           (list shell-command-switch
- (concat (file-local-name script-file) " " cmdline)))))
+                                (concat (file-local-name script-file)
+                                        " " cmdline-quote cmdline
+                                        cmdline-quote)))))
                (buffer-string))))
           (session                     ; session evaluation
             (if async





Reply via email to