Pipe Ipython magic output to a variable?

Gioelelm picture Gioelelm · Dec 20, 2013 · Viewed 12.7k times · Source

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

Answer

MattDMo picture MattDMo · Dec 20, 2013

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.