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
Notice the difference in the variable type between @MattDMo (
SList
) and @oLas (str
) answers:What about using this:
instead of the
%%bash
magic? Using the!
symbol runs the following command as a shell command, and the results are all stored inmyvar
. For running multiple commands and collecting the output of all of them, just put together a quick shell script.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:
will store the outputs in the variables
output
anderror
so that:will print to the python console: