execute Bash command from IPython execute Bash command from IPython bash bash

execute Bash command from IPython


Fernando Perez, creator of IPython, suggests this:

In [1]: %%bash. ~/.bashrc<my_fancy_bash_function> <function_argument>

This works on the current stable version (0.13.2). He admits that's a bit clunky, and welcomes pull requests. . .


If the ! implementation uses IPython.utils._process_posix.system under the hood, then it is going to use whatever which sh returns as the processing shell. This could be a true implementation of Bourne shell - it is likely Bash in some compatibility mode on many Linuxes. On my MacBook Pro it looks like it is a raw Bash shell:

In [12]: !declare -FIn [13]: !echo $BASH/bin/shIn [14]: !echo $BASH_VERSION3.2.48(1)-releaseIn [15]: import osIn [16]: os.environ['SHELL']Out[16]: '/bin/zsh'

I was hoping that it would use the $SHELL environment variable but it does not seem to today. You can probably branch the github repo, modify the ProcessHandler.sh property implementation to peek into os.environ['SHELL'] and use this if it is set instead of calling pexpect.which('sh'). Then issue a pull request.