Debian Codegraph @

docs/source/interactive/shell.rst

raw
1
2 .. note::
3
4 This page has been kept for historical reason. You most likely want to use
5 `Xonsh <https://xon.sh/>`__ instead of this.
6
7 .. _ipython_as_shell:
8
9 =========================
10 IPython as a system shell
11 =========================
12
13
14
15 Overview
16 ========
17
18 It is possible to adapt IPython for system shell usage. In the past, IPython
19 shipped a special 'sh' profile for this purpose, but it had been quarantined
20 since 0.11 release, and in 1.0 it was removed altogether. Nevertheless, much
21 of this section relies on machinery which does not require a custom profile.
22
23 You can set up your own 'sh' :ref:`profile <Profiles>` to be different from
24 the default profile such that:
25
26 * Prompt shows the current directory (see `Prompt customization`_)
27 * Make system commands directly available (in alias table) by running the
28 ``%rehashx`` magic. If you install new programs along your PATH, you might
29 want to run ``%rehashx`` to update the alias table
30 * turn ``%autocall`` to full mode
31
32
33 Environment variables
34 =====================
35
36 Rather than manipulating os.environ directly, you may like to use the magic
37 `%env` command. With no arguments, this displays all environment variables
38 and values. To get the value of a specific variable, use `%env var`. To set
39 the value of a specific variable, use `%env foo bar`, `%env foo=bar`. By
40 default values are considered to be strings so quoting them is unnecessary.
41 However, Python variables are expanded as usual in the magic command, so
42 `%env foo=$bar` means "set the environment variable foo to the value of the
43 Python variable `bar`".
44
45 Aliases
46 =======
47
48 Once you run ``%rehashx``, all of your $PATH has been loaded as IPython aliases,
49 so you should be able to type any normal system command and have it executed.
50 See ``%alias?`` and ``%unalias?`` for details on the alias facilities. See also
51 ``%rehashx?`` for details on the mechanism used to load $PATH.
52
53 .. warning::
54
55 See info at the top of the page. You most likely want to use
56 `Xonsh <https://xon.sh/>`__ instead of this.
57
58 Directory management
59 ====================
60
61 Since each command passed by IPython to the underlying system is executed
62 in a subshell which exits immediately, you can NOT use !cd to navigate
63 the filesystem.
64
65 IPython provides its own builtin ``%cd`` magic command to move in the
66 filesystem (the % is not required with automagic on). It also maintains
67 a list of visited directories (use ``%dhist`` to see it) and allows direct
68 switching to any of them. Type ``cd?`` for more details.
69
70 ``%pushd``, ``%popd`` and ``%dirs`` are provided for directory stack handling.
71
72
73 Prompt customization
74 ====================
75
76 See :ref:`custom_prompts`.
77
78
79 .. _string_lists:
80
81 String lists
82 ============
83
84 String lists (:any:`IPython.utils.text.SList`) are handy way to process output
85 from system commands. They are produced by ``var = !cmd`` syntax.
86
87 First, we acquire the output of 'ls -l'::
88
89 [Q:doc/examples]|2> lines = !ls -l
90 ==
91 ['total 23',
92 '-rw-rw-rw- 1 ville None 1163 Sep 30 2006 example-demo.py',
93 '-rw-rw-rw- 1 ville None 1927 Sep 30 2006 example-embed-short.py',
94 '-rwxrwxrwx 1 ville None 4606 Sep 1 17:15 example-embed.py',
95 '-rwxrwxrwx 1 ville None 1017 Sep 30 2006 example-gnuplot.py',
96 '-rwxrwxrwx 1 ville None 339 Jun 11 18:01 extension.py',
97 '-rwxrwxrwx 1 ville None 113 Dec 20 2006 seteditor.py',
98 '-rwxrwxrwx 1 ville None 245 Dec 12 2006 seteditor.pyc']
99
100 Now, let's take a look at the contents of 'lines' (the first number is
101 the list element number)::
102
103 [Q:doc/examples]|3> lines
104 <3> SList (.p, .n, .l, .s, .grep(), .fields() available). Value:
105
106 0: total 23
107 1: -rw-rw-rw- 1 ville None 1163 Sep 30 2006 example-demo.py
108 2: -rw-rw-rw- 1 ville None 1927 Sep 30 2006 example-embed-short.py
109 3: -rwxrwxrwx 1 ville None 4606 Sep 1 17:15 example-embed.py
110 4: -rwxrwxrwx 1 ville None 1017 Sep 30 2006 example-gnuplot.py
111 5: -rwxrwxrwx 1 ville None 339 Jun 11 18:01 extension.py
112 6: -rwxrwxrwx 1 ville None 113 Dec 20 2006 seteditor.py
113 7: -rwxrwxrwx 1 ville None 245 Dec 12 2006 seteditor.pyc
114
115 Now, let's filter out the 'embed' lines::
116
117 [Q:doc/examples]|4> l2 = lines.grep('embed',prune=1)
118 [Q:doc/examples]|5> l2
119 <5> SList (.p, .n, .l, .s, .grep(), .fields() available). Value:
120
121 0: total 23
122 1: -rw-rw-rw- 1 ville None 1163 Sep 30 2006 example-demo.py
123 2: -rwxrwxrwx 1 ville None 1017 Sep 30 2006 example-gnuplot.py
124 3: -rwxrwxrwx 1 ville None 339 Jun 11 18:01 extension.py
125 4: -rwxrwxrwx 1 ville None 113 Dec 20 2006 seteditor.py
126 5: -rwxrwxrwx 1 ville None 245 Dec 12 2006 seteditor.pyc
127
128 Now, we want strings having just file names and permissions::
129
130 [Q:doc/examples]|6> l2.fields(8,0)
131 <6> SList (.p, .n, .l, .s, .grep(), .fields() available). Value:
132
133 0: total
134 1: example-demo.py -rw-rw-rw-
135 2: example-gnuplot.py -rwxrwxrwx
136 3: extension.py -rwxrwxrwx
137 4: seteditor.py -rwxrwxrwx
138 5: seteditor.pyc -rwxrwxrwx
139
140 Note how the line with 'total' does not raise IndexError.
141
142 If you want to split these (yielding lists), call fields() without
143 arguments::
144
145 [Q:doc/examples]|7> _.fields()
146 <7>
147 [['total'],
148 ['example-demo.py', '-rw-rw-rw-'],
149 ['example-gnuplot.py', '-rwxrwxrwx'],
150 ['extension.py', '-rwxrwxrwx'],
151 ['seteditor.py', '-rwxrwxrwx'],
152 ['seteditor.pyc', '-rwxrwxrwx']]
153
154 If you want to pass these separated with spaces to a command (typical
155 for lists if files), use the .s property::
156
157
158 [Q:doc/examples]|13> files = l2.fields(8).s
159 [Q:doc/examples]|14> files
160 <14> 'example-demo.py example-gnuplot.py extension.py seteditor.py seteditor.pyc'
161 [Q:doc/examples]|15> ls $files
162 example-demo.py example-gnuplot.py extension.py seteditor.py seteditor.pyc
163
164 SLists are inherited from normal Python lists, so every list method is
165 available::
166
167 [Q:doc/examples]|21> lines.append('hey')
168
169
170 Real world example: remove all files outside version control
171 ------------------------------------------------------------
172
173 First, capture output of "hg status"::
174
175 [Q:/ipython]|28> out = !hg status
176 ==
177 ['M IPython\\extensions\\ipy_kitcfg.py',
178 'M IPython\\extensions\\ipy_rehashdir.py',
179 ...
180 '? build\\lib\\IPython\\Debugger.py',
181 '? build\\lib\\IPython\\extensions\\InterpreterExec.py',
182 '? build\\lib\\IPython\\extensions\\InterpreterPasteInput.py',
183 ...
184
185 (lines starting with ? are not under version control).
186
187 ::
188
189 [Q:/ipython]|35> junk = out.grep(r'^\?').fields(1)
190 [Q:/ipython]|36> junk
191 <36> SList (.p, .n, .l, .s, .grep(), .fields() availab
192 ...
193 10: build\bdist.win32\winexe\temp\_ctypes.py
194 11: build\bdist.win32\winexe\temp\_hashlib.py
195 12: build\bdist.win32\winexe\temp\_socket.py
196
197 Now we can just remove these files by doing 'rm $junk.s'.
198
199 The .n, .s, .p properties
200 -------------------------
201
202 Properties of :py:class:`SList <IPython.utils.text.SList>` wrapper
203 provide a convenient ways to use contained text in different formats:
204
205 * ``.n`` returns (original) string with lines separated by a newline
206 * ``.s`` returns string with lines separated by single space (for
207 convenient passing to system commands)
208 * ``.p`` returns list of "path" objects from detected file names
209
210 .. error::
211
212 You went too far scroll back up. You most likely want to use
213 `Xonsh <https://xon.sh/>`__ instead of this.
214