How to use parameters to set working directory in org mode code block

Created: — modified: — tags: org

For when you want working dorectory (:dir) be a parameter, just like variable (:var)

Org mode code blocks have a very powerful feature of being exeutable. You can type some code like this:

#+begin_src sh :results scalar
  echo "hello, my hostname is:"

Point a text cursor there, press Ctrl+C twice, and the following output appears below:

: hello, my hostname is:
: flower2

:results scalar is there to make output appear as-is, and not as table.

You can as well pass arguments to code blocks. Moreover, you can store such argument in one place, and reuse it in several code blocks:

#+name: hostname
| |

#+begin_src sh :var host=hostname :results scalar
  echo "hostname of $host is:"
  ssh root@$host hostname

: hostname of is:

You can read more about it in orgmode manual.

Moreover, you can specify working directory, and it even supports TRAMP syntax, so you can run this code on a remote machine. Of course, you can prepend your commands with ssh $host, but if you have more than a few commands, or would like to use some other programming language - using TRAMP is much better.

However, can you can't easily pass directory name as parameter.

But you can write some simple elisp helper, which will get property and format it as desired. Apparently, this is documented feature. So far I succeeded only at getting properties inside :PROPERTIES: header, but that's better than nothing! Look:

* Define your inputs here
  :user:     alexey
  :header-args: :results scalar

#+begin_src emacs-lisp :results silent
  (defun host ()
    (org-entry-get nil "host" t))
  (defun user ()
    (org-entry-get nil "user" t))
  (defun ssh_dir ()
    (format "/ssh:%s@%s:" (user) (host)))

#+begin_src sh :dir (ssh_dir)
  echo "running as user $(whoami) on host $(hostname)"
  echo "my public IP address is:"
  echo "and it resolves back to:"
  host `curl -s`

: running as user alexey on host
: my public IP address is:
: and it resolves back to:
: domain name pointer

Note, however, that since this way you'll be using headers instead of tables for "host" and "user" values, you will have to wrap them in braces, like this:

#+begin_src sh :var host=(host)
  echo "above code was executed on $host"

: above code was executed on

Update from 2020-04-20: as this StackExchange answer suggests, you can make these values available from other sections (not only the one where they are defined, with all its subsections) by using "executable code blocks" and wrapping org-entry-get into org-with-point-at org-babel-current-src-block-location, like this:

* Section One
  :hellomessage: hello

  #+NAME: get_property
  #+BEGIN_SRC emacs-lisp :var prop_name="" :results silent
  (org-with-point-at org-babel-current-src-block-location
    (org-entry-get nil prop_name t))

* Section Two

  #+HEADER: :var prop_message=get_property("hellomessage")
  #+BEGIN_SRC emacs-lisp
    (message prop_message)

  : hello

Note, however, that you will have to confirm code block execution twice.

Update from 2020-04-14: you can also get results or previous blocks like this (also posted on StackExchange):

#+name: block-1
  echo '/bin'

#+RESULTS: block-1
: /bin

#+begin_src emacs-lisp :results silent
  (defun result1 ()
      (org-babel-goto-named-result "block-1")
      (setq value (org-babel-read-result))
    (print value)

#+BEGIN_SRC sh :dir (result1) :results scalar

: /bin