Search code examples
pythonbashshellemacsipython

Ipython bash/shell cell magics: can I have persistent variables between cells?


This is my first post on SO, so please tell me if I am doing something wrong.

I am interested in using different programming languages in ipython, similar to babel/literal programming in emacs org mode. What I like about emacs org mode is that one can have multiple "cells" connecting to the same R/bash session. This allows me to re-use variables/functions created in an earlier part of the document, even if I do something else in between.

I have found that this is possible in ipython with the Rmagic. As an example

In [1]: %load_ext rpy2.ipython

In [2]: %%R
        a <- 3
        a
Out [2]: 3

In [3]: something_in_python = 'I am doing something unrelated now'

In [4]: %%R
        cat('My variable a is still here, its value is: ', a) # a is still here!

Out [4]: My variable is still here, its value is: 3

I would very much like to be able to do something similar in bash. However, no matter whether I use "%%script bash" or %%sx, variables are not persistent. Here is what I am trying to do:

In [1]: %%script bash
        var1="hello"
        echo $var1

Out[1]: hello

In [2]: %%script bash
        echo $var1 # I need $var1 to be present in this cell too - but its gone!

Out[62]: 

Is there anyway to have the same base session in multiple cells? Or at least to somehow pass on variables. Of course, I could play around with passing variables into python and then back into the next bash cell, but I have a feeling that there must be a better way. Thank you for your help!

PS: I looked for a solution, but I didn't find anything either here or through googling. There is some stuff like this: IPython Notebook previous cell content, but it doesn't seem to be helpful for my case.


Solution

  • One option, though limited, allows the stdout and stderr variables to be set. This gives you the availability of setting and passing two variables in between cells. For example:

    In [1]:%%bash --out var1 --err var2
           echo "Hello"
           echo "Hi" >&2
    
    In [2]:print(var1)
           print(var2)
    # Resulting in:
           Hello
    
           Hi
    
    In [3]:%%bash -s "$var1" "$var2"
           echo "arg1 takes on var1 = $1"
           echo "arg2 takes on var2 = $2"
    # Resulting in:
           arg1 takes on var1 = Hello
    
           arg2 takes on var2 = Hi
    

    Note that the eol character seems to make its way into the variable. You can use echo -n instead of echo to prevent the newline character from being added to the variable.

    For details on using stdout and stderr see http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/IPython%20Kernel/Script%20Magics.ipynb

    For details on passing variables back in see Can I access python variables within a `%%bash` or `%%script` ipython notebook cell?