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

Discussão em 'Python' iniciado por Stack, Abril 9, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página