Skip to content

Commit c098e57

Browse files
committed
Add examples to Bang section
1 parent 2b2b09c commit c098e57

File tree

1 file changed

+171
-8
lines changed

1 file changed

+171
-8
lines changed

README.md

+171-8
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ Nice visual cheatsheet from the [article](https://clementc.github.io/blog/2018/0
142142

143143
### Bang(!) - The History Expansion
144144
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>
145+
General notation is `'![event][:word[:modifier[:modifier]...]]'`.<br>
146146
You may ommit word separator `':'`, if the word designator begins with a `'^'`, `'$'`, `'*'`, `'-'`, or `'%'`.<br>
147147
If a word designator is supplied without an event specification, the previous command is used as the event.<br>
148148
After the optional word designator, you can add a sequence of one or more modifiers, each preceded by a `':'`.<br>
@@ -164,6 +164,15 @@ After the optional word designator, you can add a sequence of one or more modifi
164164
<td>Refer to command line <i><b>n</b></i>.</td>
165165
<td>
166166
<pre>
167+
$ history
168+
1 echo foo bar baz
169+
2 history
170+
$ !1
171+
#Print command that will be saved in history
172+
#+and executed
173+
echo foo bar baz
174+
#Actual execution
175+
foo bar baz
167176
</pre>
168177
</td>
169178
</tr>
@@ -172,6 +181,14 @@ After the optional word designator, you can add a sequence of one or more modifi
172181
<td>Refer to the command <i><b>n</b></i> lines back.</td>
173182
<td>
174183
<pre>
184+
$ history
185+
1 echo foo
186+
2 echo bar
187+
3 echo baz
188+
4 history
189+
$ !-3
190+
echo bar
191+
bar
175192
</pre>
176193
</td>
177194
</tr>
@@ -180,6 +197,11 @@ After the optional word designator, you can add a sequence of one or more modifi
180197
<td>Refer to the previous command. This is a synonym for ‘!-1’.</td>
181198
<td>
182199
<pre>
200+
$ echo foo bar baz
201+
foo bar baz
202+
$ !!
203+
echo foo bar baz
204+
foo bar baz
183205
</pre>
184206
</td>
185207
</tr>
@@ -188,6 +210,13 @@ After the optional word designator, you can add a sequence of one or more modifi
188210
<td>Refer to the most recent command preceding the current position in the history list starting with <i><b>string</b></i>.</td>
189211
<td>
190212
<pre>
213+
$printf '%s\n' foo
214+
foo
215+
$ echo bar
216+
bar
217+
$ !pri
218+
printf '%s\n' foo
219+
foo
191220
</pre>
192221
</td>
193222
</tr>
@@ -196,14 +225,30 @@ After the optional word designator, you can add a sequence of one or more modifi
196225
<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>
197226
<td>
198227
<pre>
228+
$printf '%s\n' foo
229+
foo
230+
$ echo bar
231+
bar
232+
$ !?ntf
233+
printf '%s\n' foo
234+
foo
235+
$ !?bar
236+
echo bar
237+
bar
199238
</pre>
200239
</td>
201240
</tr>
202241
<tr>
203242
<td>^<i>string1</i>^<i>tring2</i>^</td>
204243
<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>
205244
<td>
245+
For more info, refer to `s/old/new/` in <b>Modifiers</b> section.
206246
<pre>
247+
$ echo foo
248+
foo
249+
$ ^echo^printf '%s\n'^
250+
printf '%s\n' foo
251+
foo
207252
</pre>
208253
</td>
209254
</tr>
@@ -212,6 +257,13 @@ After the optional word designator, you can add a sequence of one or more modifi
212257
<td>Repeat entire command line before this event.</td>
213258
<td>
214259
<pre>
260+
$ echo foo; echo bar; !#echo baz
261+
echo foo; echo bar; echo foo; echo bar; echo baz
262+
foo
263+
bar
264+
foo
265+
bar
266+
baz
215267
</pre>
216268
</td>
217269
</tr>
@@ -223,6 +275,11 @@ After the optional word designator, you can add a sequence of one or more modifi
223275
<td>The 0th word. For many applications, this is the command word.</td>
224276
<td>
225277
<pre>
278+
$ echo foo
279+
foo
280+
$ !:0
281+
echo
282+
226283
</pre>
227284
</td>
228285
</tr>
@@ -231,6 +288,11 @@ After the optional word designator, you can add a sequence of one or more modifi
231288
<td>The <i><b>n</b></i>th word.</td>
232289
<td>
233290
<pre>
291+
$ echo foo bar baz
292+
foo bar baz
293+
$ echo !:2
294+
echo bar
295+
bar
234296
</pre>
235297
</td>
236298
</tr>
@@ -239,14 +301,24 @@ After the optional word designator, you can add a sequence of one or more modifi
239301
<td>The first argument; that is, word 1.</td>
240302
<td>
241303
<pre>
304+
$ echo foo bar baz
305+
foo bar baz
306+
$ echo !^
307+
echo foo
308+
foo
242309
</pre>
243310
</td>
244311
</tr>
245312
<tr>
246-
<td>\$</td>
313+
<td><pre>$</pre></td>
247314
<td>The last argument.</td>
248315
<td>
249316
<pre>
317+
$ echo foo bar baz
318+
foo bar baz
319+
$ echo !$
320+
echo baz
321+
baz
250322
</pre>
251323
</td>
252324
</tr>
@@ -255,6 +327,22 @@ After the optional word designator, you can add a sequence of one or more modifi
255327
<td>The word matched by the most recent `?string?` search</td>
256328
<td>
257329
<pre>
330+
$ echo foo
331+
foo
332+
$ printf '%s\n' bar
333+
bar
334+
$ !?ch
335+
echo foo
336+
foo
337+
$ !% baz
338+
echo baz
339+
baz
340+
$ !?bar
341+
printf '%s\n' bar
342+
bar
343+
$ echo !%
344+
echo bar
345+
bar
258346
</pre>
259347
</td>
260348
</tr>
@@ -263,6 +351,14 @@ After the optional word designator, you can add a sequence of one or more modifi
263351
<td>A range of words; `-y` abbreviates `0-y`.</td>
264352
<td>
265353
<pre>
354+
$ echo foo bar baz
355+
foo bar baz
356+
$ echo !:2-3
357+
echo bar baz
358+
bar baz
359+
$ !:-1
360+
echo bar
361+
bar
266362
</pre>
267363
</td>
268364
</tr>
@@ -271,6 +367,13 @@ After the optional word designator, you can add a sequence of one or more modifi
271367
<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>
272368
<td>
273369
<pre>
370+
$ echo foo bar baz
371+
foo bar baz
372+
$ printf '%s\n' !*
373+
printf '%s\n' foo bar baz
374+
foo
375+
bar
376+
baz
274377
</pre>
275378
</td>
276379
</tr>
@@ -279,6 +382,12 @@ After the optional word designator, you can add a sequence of one or more modifi
279382
<td>Abbreviates `x-$`</td>
280383
<td>
281384
<pre>
385+
$ echo foo bar baz
386+
foo bar baz
387+
$ printf '%s\n' !:2*
388+
printf '%s\n' bar baz
389+
bar
390+
baz
282391
</pre>
283392
</td>
284393
</tr>
@@ -287,6 +396,13 @@ After the optional word designator, you can add a sequence of one or more modifi
287396
<td>Abbreviates `x-$` like `x*`, but omits the last word.</td>
288397
<td>
289398
<pre>
399+
$ echo foo bar baz
400+
foo bar baz
401+
$ printf '%s\n' !:0-
402+
printf '%s\n' echo foo bar
403+
echo
404+
foo
405+
bar
290406
</pre>
291407
</td>
292408
</tr>
@@ -295,41 +411,64 @@ After the optional word designator, you can add a sequence of one or more modifi
295411
</tr>
296412
<tr>
297413
<td>p</td>
298-
<td>Print the new command but do not execute it.</td>
414+
<td>Print the new command but do not execute it.<br>Printed command is saved in history, so you can use <kbd>Ctrl+p</kbd> to re-enter it in current prompt.</td>
299415
<td>
300416
<pre>
417+
$ echo foo bar baz
418+
foo bar baz
419+
$ !:p
420+
#Printed, but not executed
421+
echo foo bar baz
422+
$ !:*:p
423+
foo bar baz
301424
</pre>
302425
</td>
303426
</tr>
304427
<tr>
305428
<td>h</td>
306-
<td>Remove a trailing pathname component, leaving only the head (Actually, remove all after last `/`).</td>
429+
<td>Remove a trailing pathname component, leaving only the head (Actually, remove all after last `/`, including).</td>
307430
<td>
308431
<pre>
432+
$ echo foo /example/path/bar.txt baz
433+
foo /example/path/bar.txt baz
434+
$ !:p:h
435+
echo foo /example/path
309436
</pre>
310437
</td>
311438
</tr>
312439
<tr>
313440
<td>t</td>
314-
<td>Remove all leading pathname components, leaving the tail (Actually, remove all before last `/`).</td>
441+
<td>Remove all leading pathname components, leaving the tail (Actually, remove all before last `/`, including).</td>
315442
<td>
316443
<pre>
444+
$ echo foo /example/path/bar.txt baz
445+
foo /example/path/bar.txt baz
446+
$ !:p:t
447+
bar.txt baz
317448
</pre>
318449
</td>
319450
</tr>
320451
<tr>
321452
<td>r</td>
322-
<td>Remove a trailing suffix of the form `.suffix`, leaving the basename (Actually, remove all after last `.`).</td>
453+
<td>Remove a trailing suffix of the form `.suffix`, leaving the basename (Actually, remove all after last `.`, including).</td>
323454
<td>
324455
<pre>
456+
$ echo foo /example/path/bar.txt baz
457+
foo /example/path/bar.txt baz
458+
$ !:p:r
459+
echo foo /example/path/bar
325460
</pre>
326461
</td>
327462
</tr>
328463
<tr>
329464
<td>e</td>
330-
<td>Remove all but the trailing suffix (Actually, remove all before last `.`).</td>
465+
<td>Remove all but the trailing suffix (Actually, remove all before last `.`, including).</td>
331466
<td>
332467
<pre>
468+
$ echo foo /example/path/bar.txt baz
469+
foo /example/path/bar.txt baz
470+
$ !:p:e
471+
txt baz
333472
</pre>
334473
</td>
335474
</tr>
@@ -338,6 +477,10 @@ After the optional word designator, you can add a sequence of one or more modifi
338477
<td>Quote the substituted words, escaping further substitutions.</td>
339478
<td>
340479
<pre>
480+
$ echo foo 'bar baz'
481+
foo bar baz
482+
$ !:p:q
483+
'echo foo '\'bar baz'\'''
341484
</pre>
342485
</td>
343486
</tr>
@@ -346,6 +489,10 @@ After the optional word designator, you can add a sequence of one or more modifi
346489
<td>Quote the substituted words as with ‘q’, but break into words at spaces, tabs, and newlines.</td>
347490
<td>
348491
<pre>
492+
$ echo foo 'bar baz'
493+
foo bar baz
494+
$ !:p:x
495+
'echo' 'foo' ''\'bar' 'baz'\'''
349496
</pre>
350497
</td>
351498
</tr>
@@ -354,6 +501,10 @@ After the optional word designator, you can add a sequence of one or more modifi
354501
<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>
355502
<td>
356503
<pre>
504+
$ echo foo bar
505+
foo bar
506+
$ !:p:s/foo/baz
507+
echo baz bar
357508
</pre>
358509
</td>
359510
</tr>
@@ -362,6 +513,14 @@ After the optional word designator, you can add a sequence of one or more modifi
362513
<td>Repeat the previous substitution.</td>
363514
<td>
364515
<pre>
516+
$ echo foo bar
517+
foo bar
518+
$ !:p:s/foo/baz
519+
echo baz bar
520+
$ printf '%s\n' foo
521+
foo
522+
$ !:p:&
523+
printf '%s\n' baz
365524
</pre>
366525
</td>
367526
</tr>
@@ -370,13 +529,17 @@ After the optional word designator, you can add a sequence of one or more modifi
370529
<td>Cause changes to be applied over the entire event line. Used in conjunction with `s`, as in gs/old/new/, or with `&`.</td>
371530
<td>
372531
<pre>
532+
$ echo foo bar foo
533+
foo bar foo
534+
$ !:p:gs/foo/baz
535+
echo baz bar baz
373536
</pre>
374537
</td>
375538
</tr>
376539
<tr>
377540
<td>G</td>
378541
<td>Apply the following ‘s’ modifier once to each word in the event.</td>
379-
<td>
542+
<td>Result is same as in `g` modifier
380543
<pre>
381544
</pre>
382545
</td>

0 commit comments

Comments
 (0)