execute Bash command from IPython

2020-05-18 23:57发布

问题:

I want to source my bash environment when executing a bash command from IPython using the ! operator, thus allowing me access to my defined bash functions:

In[2]: !<my_fancy_bash_function> <function_argument>

currently IPython is sourcing sh rather than bash:

In[3]: !declare -F
sh: 1: declare: not found

How to source bash and set my environment settings from IPython?

回答1:

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. . .



回答2:

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 -F

In [13]: !echo $BASH
/bin/sh

In [14]: !echo $BASH_VERSION
3.2.48(1)-release

In [15]: import os

In [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.



标签: bash ipython