IPython ships with a special profile called pysh, which you can activate at the command line as `ipython -p pysh'. This loads InterpreterExec, along with some additional facilities and a prompt customized for filesystem navigation.
Note that this does not make IPython a full-fledged system shell. In particular, it has no job control, so if you type Ctrl-Z (under Unix), you'll suspend pysh itself, not the process you just started.
What the shell profile allows you to do is to use the convenient and powerful syntax of Python to do quick scripting at the command line. Below we describe some of its features.
All of your $PATH has been loaded as IPython aliases, so you should be able to type any normal system command and have it executed. See %alias? and %unalias? for details on the alias facilities. See also %rehash? and %rehashx? for details on the mechanism used to load $PATH.
Any lines which begin with `~', `/' and `.' will be executed as shell commands instead of as Python code. The special escapes below are also recognized. !cmd is valid in single or multi-line input, all others are only valid in single-line input:
A few brief examples will illustrate these (note that the indentation below may be incorrectly displayed):
fperez[~/test]|3> !ls *s.py
scopes.py strings.py
ls is an internal alias, so there's no need to use !:
fperez[~/test]|4> ls *s.py
scopes.py* strings.py
!!ls will return the output into a Python variable:
fperez[~/test]|5> !!ls *s.py
<5> ['scopes.py', 'strings.py']
fperez[~/test]|6> print _5
['scopes.py', 'strings.py']
$ and $$ allow direct capture to named variables:
fperez[~/test]|7> $astr = ls *s.py
fperez[~/test]|8> astr
<8> 'scopes.py\nstrings.py'
fperez[~/test]|9> $$alist = ls *s.py
fperez[~/test]|10> alist
<10> ['scopes.py', 'strings.py']
alist is now a normal python list you can loop over. Using $ will expand back the python values when alias calls are made:
fperez[~/test]|11> for f in alist:
|..> print 'file',f,
|..> wc -l $f
|..>
file scopes.py 13 scopes.py
file strings.py 4 strings.py
Note that you may need to protect your variables with braces if you want to append strings to their names. To copy all files in alist to .bak extensions, you must use:
fperez[~/test]|12> for f in alist:
|..> cp $f ${f}.bak
If you try using $f.bak, you'll get an AttributeError exception saying that your string object doesn't have a .bak attribute. This is because the $ expansion mechanism allows you to expand full Python expressions:
fperez[~/test]|13> echo "sys.platform is: $sys.platform"
sys.platform is: linux2
IPython's input history handling is still active, which allows you to rerun a single
block of multi-line input by simply using exec:
fperez[~/test]|14> $$alist = ls *.eps
fperez[~/test]|15> exec _i11
file image2.eps 921 image2.eps
file image.eps 921 image.eps
While these are new special-case syntaxes, they are designed to allow very efficient use of the shell with minimal typing. At an interactive shell prompt, conciseness of expression wins over readability.
The os, sys and shutil modules from the Python standard library are automatically loaded. Some additional functions, useful for shell usage, are listed below. You can request more help about them with `?'.
Since each command passed by pysh to the underlying system is executed in a subshell which exits immediately, you can NOT use !cd to navigate the filesystem.
Pysh provides its own builtin `%cd' magic command to move in the filesystem (the % is not required with automagic on). It also maintains a list of visited directories (use %dhist to see it) and allows direct switching to any of them. Type `cd?' for more details.
%pushd, %popd and %dirs are provided for directory stack handling.
The supplied ipythonrc-pysh profile comes with an example of a very colored and detailed prompt, mainly to serve as an illustration. The valid escape sequences, besides color names, are:
Fernando Perez 2006-06-06