2

How to share variables' values in org-mode between different sessions?

Simple example: in session one I create _gpg_tmpdir

#+name: make_temporary_directories
#+begin_src bash :session *one*
_gpg_tmpdir="$( mktemp -d )"
#+end_src

and need to clean up it in session two:

#+name: clean_temporary_directories
#+begin_src bash :session *two*
rm -rf $_gpg_tmpdir
#+end_src

The example is for the demonstration purpose only. The question is what is the less painfull way to share variables between different code sessions (perhaps with different code languages) in org-mode.

3
  • Is this an XY problem? en.wikipedia.org/wiki/XY_problem Maybe clarify why you want to do this. The entire point of sessions is to keep things separate. If you must communicate between the two, this may not be an Org question but a general how do I share data between bash processes question. Commented Feb 12, 2020 at 22:55
  • You are right. I share data between bash sessions with something like set > .state-0000 and source .state-0000. It works but I don't like it. Commented Feb 12, 2020 at 23:23
  • Many systems use something like that. State files, when filed correctly are fine. It's no worse than anything else. Everything is a file... Commented Feb 13, 2020 at 13:40

1 Answer 1

4

You can use RESULTS for the first session to write out the results, and :var in the second session to import results.

#+name: make_temporary_directories
#+begin_src bash :session *one* :results output
_gpg_tmpdir="$( mktemp -d )"
echo $_gpg_tmpdir
#+end_src
#+RESULTS: make_temporary_directories
: /tmp/tmp.iAE5oSlwcC
#+name: clean_temporary_directories
#+begin_src bash :session *two* :var gpg_tmpdir=make_temporary_directories :results output
echo $gpg_tmpdir
#+end_src
3
  • Ok. The next question is: how to share results of one bash script in another? I want to tangle different logical sessions to different scripts. Commented Feb 13, 2020 at 8:10
  • ... and how to get rid of PS1 in results (I don't know why I get it): : trupanka@loststation ~/CLASSES/_PLAYGROUND/BASH $ /tmp/tmp.7QMo1uK8Up Commented Feb 13, 2020 at 8:14
  • ask your next question as a new question, the short answer is to use the :tangle header arg to send different babel blocks to different files. Commented Feb 13, 2020 at 21:33

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.