0

By default my source code block can't find most executables:

#+BEGIN_SRC bash
which aws
#+END_SRC

#+RESULTS:

This fixes the problem:

#+BEGIN_SRC bash
source $HOME/.bash_profile
which aws
#+END_SRC

#+RESULTS:
| ];                                    | PHP | 7.1.25 |  |
| /Users/bwood/Library/Python/3.7/bin/aws |     |        |    |

My question: Is there a way to make code blocks of language type bash (or shell) always source my .bash_profile, without having to add that line at the beginning of every source block?

Base on https://orgmode.org/manual/prologue.html I tried adding the following to my init file, but this didn't fix the problem so maybe I'm not understanding what prologue does:

(add-to-list 'org-babel-default-header-args:shell
             '((:prologue . "source /Users/bwood/.bash_profile")))

EDIT: I should have mentioned that I'm running graphical UI Emacs under MacOS.

  • (I'd also be interested to know how to prevent that 2nd Results block from being formatted as a table.) – Brian Wood Nov 15 at 19:14
  • 1
    When you login, your shell or your Desktop environment should be sourcing your .profile (or ~/.bash_profile) file automatically. When you start emacs frrom such an environment, it will inherit env variables that are exported (PATH is automatically exported). Then any subprocess that emacs starts (e.g. the shell that org-babel invokes) will know about your PATH. The question is then why this breaks down in your case. When you login and start emacs, try M-x getenv RET PATH RET; if that does not give you the PATH you expect, then the problem is outside emacs, so trying to solve it ... – NickD Nov 16 at 3:12
  • ... within emacs is bound to be an unsatisfactory solution. It would be better to find and fix the root cause of the problem. That is however outside the scope of Emacs SE: I would suggest the Unix-Linux SE site. – NickD Nov 16 at 3:14
  • @NickD There is at least one situation I am aware of where this needs to be handled within Emacs. ChromeOS can run Linux in a VM and this has some integration with the outer OS, in particular, icons than launch Linux GUI applications are added to the system tray. If you run Emacs from that icon, it does not pass through a shell at all, and you don't have your environment variables. – Omar Nov 23 at 17:27
  • > (I'd also be interested to know how to prevent that 2nd Results block from being formatted as a table.) After installing the exec-path-from-shell I no longer get this table formatting. – Brian Wood Nov 24 at 0:31
2

Try this package exec-path-from-shell which should solve your problem.

  • Thanks! That package fixed it! – Brian Wood Nov 24 at 0:30

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.