execute Bash command from IPython

前端 未结 2 1476
悲&欢浪女
悲&欢浪女 2021-02-01 06:05

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:

相关标签:
2条回答
  • 2021-02-01 06:39

    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.

    0 讨论(0)
  • 2021-02-01 06:45

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

    0 讨论(0)
提交回复
热议问题