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?

cammil
  • 509
  • 3
  • 12

1 Answers1

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.

xuchunyang
  • 14,302
  • 1
  • 18
  • 39