3

I don't understand how the source code blocks work exactly.

There are some variables, definitions and aliases in my .bashrc that I think I should be able to use in the source code blocks. However, when I run something like the following, the output suggests that my .bashrc is not being run.

#+BEGIN_SRC bash :results output
echo $0
echo $VAR_DEFINED_IN_BASHRC
#+END_SRC

#+RESULTS:
: bash
: 

Is this expected behaviour? How do I use the things in my bashrc file in source code blocks?

1
  • from what I understand Emacs will not read your aliases Commented Nov 24, 2018 at 20:45

1 Answer 1

6

You can find out how your code is executed by checking ob-shell.el. As for your particular example, your code is passed to bash as stdin, something like the following

$ echo 'echo $VAR_DEFINED_IN_BASHRC' | bash

It will not work, you need the following instead

$ echo 'echo $VAR_DEFINED_IN_BASHRC' | bash -i

So you need to run bash interactively (-i) to source your .bashrc, your can use :shebang argument, then org will create a script (i.e., executable file) and execute it directly via operation system.

#+BEGIN_SRC sh :shebang #!/bin/bash -i :results output
echo $VAR_DEFINED_IN_BASHRC
#+END_SRC

#+RESULTS:
: 42

In my .bashrc, VAR_DEFINED_IN_BASHRC is set to 42.

1
  • this is an excellent answer. i wonder if there's an easy to also use bash and -i option. Commented Apr 6, 2019 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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