41

I want to run a bash script in my ipython Notebook and save the output as a string in a python variable for further manipulation. Basically I want to pipe the output of the bash magic to a variable, For example the output of something like this:

%%bash
some_command [options] foo bar
MattDMo
  • 100,794
  • 21
  • 241
  • 231
Gioelelm
  • 2,645
  • 5
  • 30
  • 49

3 Answers3

71

What about using this:

myvar = !some_command --option1 --option2 foo bar

instead of the %%bash magic? Using the ! symbol runs the following command as a shell command, and the results are all stored in myvar. For running multiple commands and collecting the output of all of them, just put together a quick shell script.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • 4
    Is it possible to differentiate between stdout and stderr? – Vinayak Kaniyarakkal Mar 27 '18 at 10:17
  • @Vinayak With the `%%capture` cell magics you can distinguish stdout and stderr easily. See [here](http://ipython.readthedocs.io/en/stable/interactive/magics.html#cellmagic-capture) and [here](http://ipython.readthedocs.io/en/stable/api/generated/IPython.utils.capture.html). Looks akin to oLas's answer below but offers another route. – Wayne May 02 '18 at 14:40
30

For completeness, if you would still like to use the %%bash cell magic, you can pass the --out and --err flags to redirect the outputs from the stdout and stderr to a variable of your choice.

From the doucumentation:

%%bash --out output --err error
echo "hi, stdout"
echo "hello, stderr" >&2

will store the outputs in the variables output and error so that:

print(error)
print(output)

will print to the python console:

hello, stderr
hi, stdout
oLas
  • 1,171
  • 1
  • 9
  • 17
13

Notice the difference in the variable type between @MattDMo (SList) and @oLas (str) answers:

In [1]: output = !whoami

In [2]: type(output)
Out[2]: IPython.utils.text.SList

In [3]: %%bash --out output
   ...: whoami

In [4]: type(output)
Out[4]: str
Aziz Alto
  • 19,057
  • 5
  • 77
  • 60