1

The org manual specifies in the this link: https://orgmode.org/manual/Languages.html#Languages the language it supports (including shell). It mentions this:

Additional documentation for some languages is at https://orgmode.org/worg/org-contrib/babel/languages.html

The above link points further this page: https://orgmode.org/worg/org-contrib/babel/languages/index.html

But unfortunately it doesn't have any documentation for the shell language.

What I'm looking is to see if I can change the working directory where it is executed. Something like this:

#+begin_src sh working-dir:/home/some_dir #working-dir doesn't work obviously.
ls
#+end_src

But I'm not able to find documentation on where to find the header arguments for a specific language.

3
  • You could cd from the first line within the code block? Indeed I did not find much documentation for babel shell. Of course you can check out ob-shell.el. Dec 14 '20 at 11:42
  • I could do that, but I do want to know the different header arguments supported by shell. I could look the source code, but I want that to be the last resort. :-)
    – Sibi
    Dec 14 '20 at 12:15
  • Doesn't :dir work? I believe it should work even for remote directories. And that's a general mechanism, not restricted to shell - so it probably is documented in the generic header options section, not a language-specific one.
    – NickD
    Dec 14 '20 at 14:28
3

What you are looking for is the :dir header argument:

#+begin_src shell :dir /home/nick/src
pwd
#+end_src

#+RESULTS:
: /home/nick/src


#+begin_src shell :dir /home/nick/src/github
pwd
#+end_src

#+RESULTS:
: /home/nick/src/github/pbench


#+begin_src python :dir /home/nick/src/
  import os
  pwd = os.getcwd()
  return pwd
#+end_src

#+RESULTS:
: /home/nick/src

Note that it is a language-independent header argument, so you won't find it documented under a specific language: look in the Org mode manual for it:

+begin_src emacs-lisp
(info "(org) Environment of a code block")
#+end_src

and search for the "Choosing a working directory" subsection (or look for the same thing in the online manual.)

Note also that you can use a remote host spec for it, in which case babel will use Tramp to execute the block on the remote host:

#+begin_src shell :dir /scp:foo.bar.org:/home/nick/src :results drawer
hostname
pwd
#+end_src

#+RESULTS:
:results:
foo.bar.org
/home/nick/src
:end:



#+begin_src python :dir /scp:foo.bar.org:/home/nick/src/ :results drawer
  import os
  import socket
  pwd = os.getcwd()
  return (socket.gethostname(), pwd)
#+end_src

#+RESULTS:
:results:
('foo.bar.org', '/home/nick/src')
:end:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.