I want to execute sudo command in org-babel like this:

#+begin_src shell :dir "/sudo::" :cache no
sudo make install

It works fine. It use TRAMP /sudo: method.

But I need to execute sudo command relative under current working directory. As following block shows:

#+begin_src shell :dir "/sudo::data/code/quirc" :cache no
sudo make install

But it does not work. It is under /root instead of current working directory. So how can I execute sudo command under a specific directory as in org-babel like upper?

EDIT: Might add an advice to change TRAMP sudo method default directory to babel src block's directory? Don't know how to change this in TRAMP code. Have not found any tramp-default-directory related variables.

  • Have you tried with a trailing slash at the end of a path? – user12563 Mar 1 '18 at 10:23
  • With a trailing slash like "/sudo::data/code/quirc/" does not work. – stardiviner Mar 2 '18 at 3:44

You must use an absolute local directory path. Like this:

#+begin_src shell :dir "/sudo::/data/code/quirc" :cache no
sudo make install

See the leading slash in /data/code/quirc.

Edit: You must recompute the directory for the root shell. Something like this:

#+begin_src shell :dir "/sudo::" :var dir=(expand-file-name "data/code/quirc") :cache no
cd $dir
make install
| improve this answer | |
  • What I want to execute under a relative path not absolute path. As my question described. – stardiviner Mar 2 '18 at 3:43
  • /sudo::data/code/quirc means a relative directory under the home dir of user root. And this is /root. – Michael Albinus Mar 2 '18 at 7:43
  • Yeah, I know that. I need to relative directory under current working directory. – stardiviner Mar 2 '18 at 12:29
  • You mean the relative directory under current working directory, but with sudo? – Michael Albinus Mar 3 '18 at 14:39
  • yes. that what I need. – stardiviner Mar 6 '18 at 4:23

I suddenly come up with string concat idea.

Here is the very simple solution.

#+begin_src sh :dir (concat "/sudo::" (expand-file-name "data/code"))

#+RESULTS[(2020-07-20 10:34:50) b20fc4ca99ae523b3497adaa26b8945c57b06254]:
: /home/stardiviner/Org/Wiki/Computer Technology/Programming/Emacs/Data/Emacs Packages/Org Mode/data/code
| improve this answer | |
  • This is a better solution than mine above. It's cleaner not requiring a local variable. It seems that keeping the current directory should be the default behavior. – Yu Shen Jul 21 at 4:05
  • I original indeed have this idea. But I found it's not a good idea. I digged the ob-core.el source code, it's not good to keep current default directory for remote path like tramp method path. For example :dir /ssh:user@host:data/code Like this remote path should not keep default directory. There is a complex solution, add a new header argument or emacs defcustom option to control this behavior. But it's not trivial. So currently my solution should be the simpiest and elegant now. – stardiviner Jul 22 at 4:29

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.