33

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?

2 Answers 2

39

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

Sign up to request clarification or add additional context in comments.

Comments

30

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.

3 Comments

great reply, thanks. What, in your view, would be a good way of setting up the environment in the spawned process which is coming from the ! implementation (linked to in your answer)? Doing a quick 'ghetto' test of symlinking /bin/bash to /bin/sh and issuing ! source ~/.bashrc && declare -F yields nothing, whereas ! env && declare -F correctly prints out my declared variables, but the bash functions are still not accessible...any ideas what is going on there?
On recent version you can use the %%bash cell magic to execute multiline bash.
Discovered something quite amazing... you can use the simple bash variable expansion tricks to expand a variable declared in python! For example extension=".txt" followed by !ls *"$extension" prints all .txt files!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.