You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+240-30
Original file line number
Diff line number
Diff line change
@@ -140,41 +140,251 @@ Nice visual cheatsheet from the [article](https://clementc.github.io/blog/2018/0
140
140
### Process control
141
141
142
142
143
-
### Bang(!)
144
-
Bash also has some handy features that use the ! (bang) to allow you to do some funky stuff with bash commands.
143
+
### Bang(!) - The History Expansion
144
+
Bash also has some handy features that use the ! (bang) to allow you to do some funky stuff with bash commands.<br>
145
+
General notation is `'![event][:word[:modifier]]'`.<br>
146
+
You may ommit word separator `':'`, if the word designator begins with a `'^'`, `'$'`, `'*'`, `'-'`, or `'%'`.<br>
147
+
If a word designator is supplied without an event specification, the previous command is used as the event.<br>
148
+
After the optional word designator, you can add a sequence of one or more modifiers, each preceded by a `':'`.<br>
145
149
146
150
<table>
147
-
<tr>
148
-
<td>!!</td>
149
-
<td>run last command</td>
150
-
</tr>
151
-
<tr>
152
-
<td>!blah</td>
153
-
<td>run the most recent command that starts with ‘blah’ (e.g. !ls)</td>
154
-
</tr>
155
-
<tr>
156
-
<td>!blah:p</td>
157
-
<td>print out the command that !blah would run (also adds it as the latest command in the command history)</td>
158
-
</tr>
159
-
<tr>
160
-
<td>!$</td>
161
-
<td>the last word of the previous command (same as Alt + .)</td>
162
-
</tr>
163
-
<tr>
164
-
<td>!$:p</td>
165
-
<td>print out the word that !$ would substitute</td>
166
-
</tr>
167
-
<tr>
168
-
<td>!*</td>
169
-
<td>the previous command except for the last word (e.g. if you type ‘find some_file.txt /‘, then !* would give you ‘find some_file.txt‘)</td>
170
-
</tr>
171
-
<tr>
172
-
<td>!*:p</td>
173
-
<td>print out what !* would substitute</td>
174
-
</tr>
151
+
<tr>
152
+
<th>Events</th><th>Meaning</th><th>Example</th>
153
+
</tr>
154
+
<tr>
155
+
<td>!</td>
156
+
<td>Start a history substitution, except when followed by a space, tab, the end of the line, ‘=’ or ‘(’ (when the extglob shell option is enabled using the shopt builtin).</td>
157
+
<td>
158
+
<pre>
159
+
</pre>
160
+
</td>
161
+
</tr>
162
+
<tr>
163
+
<td>!<i>n</i></td>
164
+
<td>Refer to command line <i><b>n</b></i>.</td>
165
+
<td>
166
+
<pre>
167
+
</pre>
168
+
</td>
169
+
</tr>
170
+
<tr>
171
+
<td>!-<i>n</i></td>
172
+
<td>Refer to the command <i><b>n</b></i> lines back.</td>
173
+
<td>
174
+
<pre>
175
+
</pre>
176
+
</td>
177
+
</tr>
178
+
<tr>
179
+
<td>!!</td>
180
+
<td>Refer to the previous command. This is a synonym for ‘!-1’.</td>
181
+
<td>
182
+
<pre>
183
+
</pre>
184
+
</td>
185
+
</tr>
186
+
<tr>
187
+
<td>!<i>string</i></td>
188
+
<td>Refer to the most recent command preceding the current position in the history list starting with <i><b>string</b></i>.</td>
189
+
<td>
190
+
<pre>
191
+
</pre>
192
+
</td>
193
+
</tr>
194
+
<tr>
195
+
<td>!?<i>string</i>[?]</td>
196
+
<td>Refer to the most recent command preceding the current position in the history list containing <i><b>string</b></i>. The trailing ‘?’ may be omitted if the <i><b>string</b></i> is followed immediately by a newline.</td>
197
+
<td>
198
+
<pre>
199
+
</pre>
200
+
</td>
201
+
</tr>
202
+
<tr>
203
+
<td>^<i>string1</i>^<i>tring2</i>^</td>
204
+
<td>Quick Substitution. Repeat the last command, replacing <i><b>string1</b></i> with <i><b>string2</b></i>. Equivalent to `!!:s/string1/string2`.</td>
205
+
<td>
206
+
<pre>
207
+
</pre>
208
+
</td>
209
+
</tr>
210
+
<tr>
211
+
<td>!#</td>
212
+
<td>Repeat entire command line before this event.</td>
213
+
<td>
214
+
<pre>
215
+
</pre>
216
+
</td>
217
+
</tr>
218
+
<tr>
219
+
<th>Words</th><th>Meaning</th><th>Example</th>
220
+
</tr>
221
+
<tr>
222
+
<td>0 (zero)</td>
223
+
<td>The 0th word. For many applications, this is the command word.</td>
224
+
<td>
225
+
<pre>
226
+
</pre>
227
+
</td>
228
+
</tr>
229
+
<tr>
230
+
<td><i>n</i></td>
231
+
<td>The <i><b>n</b></i>th word.</td>
232
+
<td>
233
+
<pre>
234
+
</pre>
235
+
</td>
236
+
</tr>
237
+
<tr>
238
+
<td>^</td>
239
+
<td>The first argument; that is, word 1.</td>
240
+
<td>
241
+
<pre>
242
+
</pre>
243
+
</td>
244
+
</tr>
245
+
<tr>
246
+
<td>\$</td>
247
+
<td>The last argument.</td>
248
+
<td>
249
+
<pre>
250
+
</pre>
251
+
</td>
252
+
</tr>
253
+
<tr>
254
+
<td>%</td>
255
+
<td>The word matched by the most recent `?string?` search</td>
256
+
<td>
257
+
<pre>
258
+
</pre>
259
+
</td>
260
+
</tr>
261
+
<tr>
262
+
<td>x-y</td>
263
+
<td>A range of words; `-y` abbreviates `0-y`.</td>
264
+
<td>
265
+
<pre>
266
+
</pre>
267
+
</td>
268
+
</tr>
269
+
<tr>
270
+
<td>*</td>
271
+
<td>All of the words, except the 0th. This is a synonym for `1-$`. It is not an error to use `*` if there is just one word in the event - the empty string is returned in that case.</td>
272
+
<td>
273
+
<pre>
274
+
</pre>
275
+
</td>
276
+
</tr>
277
+
<tr>
278
+
<td>x*</td>
279
+
<td>Abbreviates `x-$`</td>
280
+
<td>
281
+
<pre>
282
+
</pre>
283
+
</td>
284
+
</tr>
285
+
<tr>
286
+
<td>x-</td>
287
+
<td>Abbreviates `x-$` like `x*`, but omits the last word.</td>
<td>Print the new command but do not execute it.</td>
299
+
<td>
300
+
<pre>
301
+
</pre>
302
+
</td>
303
+
</tr>
304
+
<tr>
305
+
<td>h</td>
306
+
<td>Remove a trailing pathname component, leaving only the head (Actually, remove all after last `/`).</td>
307
+
<td>
308
+
<pre>
309
+
</pre>
310
+
</td>
311
+
</tr>
312
+
<tr>
313
+
<td>t</td>
314
+
<td>Remove all leading pathname components, leaving the tail (Actually, remove all before last `/`).</td>
315
+
<td>
316
+
<pre>
317
+
</pre>
318
+
</td>
319
+
</tr>
320
+
<tr>
321
+
<td>r</td>
322
+
<td>Remove a trailing suffix of the form `.suffix`, leaving the basename (Actually, remove all after last `.`).</td>
323
+
<td>
324
+
<pre>
325
+
</pre>
326
+
</td>
327
+
</tr>
328
+
<tr>
329
+
<td>e</td>
330
+
<td>Remove all but the trailing suffix (Actually, remove all before last `.`).</td>
331
+
<td>
332
+
<pre>
333
+
</pre>
334
+
</td>
335
+
</tr>
336
+
<tr>
337
+
<td>q</td>
338
+
<td>Quote the substituted words, escaping further substitutions.</td>
339
+
<td>
340
+
<pre>
341
+
</pre>
342
+
</td>
343
+
</tr>
344
+
<tr>
345
+
<td>x</td>
346
+
<td>Quote the substituted words as with ‘q’, but break into words at spaces, tabs, and newlines.</td>
347
+
<td>
348
+
<pre>
349
+
</pre>
350
+
</td>
351
+
</tr>
352
+
<tr>
353
+
<td><i>s/<i>old</i>/<i>new</i>/</i></td>
354
+
<td>Substitute <i><b>new</b></i> for the first occurrence of <i><b>old</b></i> in the event line. Any delimiter may be used in place of `/`. The delimiter may be quoted in <i><b>old</b></i> and <i><b>new</b></i> with a single backslash. If `&` appears in <i><b>new</b></i>, it is replaced by <i><b>old</b></i>. A single backslash will quote the `&`. The final delimiter is optional if it is the last character on the input line.</td>
355
+
<td>
356
+
<pre>
357
+
</pre>
358
+
</td>
359
+
</tr>
360
+
<tr>
361
+
<td>&</td>
362
+
<td>Repeat the previous substitution.</td>
363
+
<td>
364
+
<pre>
365
+
</pre>
366
+
</td>
367
+
</tr>
368
+
<tr>
369
+
<td>g<br>a</td>
370
+
<td>Cause changes to be applied over the entire event line. Used in conjunction with `s`, as in gs/old/new/, or with `&`.</td>
371
+
<td>
372
+
<pre>
373
+
</pre>
374
+
</td>
375
+
</tr>
376
+
<tr>
377
+
<td>G</td>
378
+
<td>Apply the following ‘s’ modifier once to each word in the event.</td>
379
+
<td>
380
+
<pre>
381
+
</pre>
382
+
</td>
383
+
</tr>
175
384
</table>
176
385
177
386
## Recent links
178
387
*[Bash Shortcuts For Maximum Productivity](http://www.skorks.com/2009/09/bash-shortcuts-for-maximum-productivity/)
0 commit comments