-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash-magic.html
494 lines (436 loc) · 29.2 KB
/
bash-magic.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="preconnect" href="https://rsms.me/">
<link rel="preload" href="https://rsms.me/inter/font-files/Inter-SemiBold.woff2?v=4.0" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="https://rsms.me/inter/font-files/Inter-Regular.woff2?v=4.0" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="https://rsms.me/inter/font-files/Inter-Medium.woff2?v=4.0" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="https://rsms.me/inter/font-files/InterVariable.woff2?v=4.0" as="font" type="font/woff2" crossorigin>
<link rel="stylesheet" href="https://rsms.me/inter/inter.css">
<link href="/feeds/atom.xml" type="application/atom+xml" rel="alternate" title="All articles Atom feed" />
<link href="/feeds/rss.xml" type="application/rss+xml" rel="alternate" title="All articles RSS feed" />
<meta name="author" content="tome.one">
<meta name="description" content="Over the years I have learned many tips and tricks when working with Bash. Some are well known while others are not. Here are a few tricks that I find the most useful. Searching history So you know you just typed that command but cannot remember what it was. Fine …">
<meta name="keywords" content="linux, bash">
<meta property="og:site_name" content="tome.one">
<meta property="og:title" content="Bash Magic">
<meta property="og:description" content="Over the years I have learned many tips and tricks when working with Bash. Some are well known while others are not. Here are a few tricks that I find the most useful. Searching history So you know you just typed that command but cannot remember what it was. Fine …">
<meta property="og:locale" content="en_US">
<meta property="og:url" content="./bash-magic.html">
<meta property="og:type" content="article">
<meta property="article:published_time" content="2016-12-02 17:50:00+01:00">
<meta property="article:modified_time" content="">
<meta property="article:author" content="./author/tomeone.html">
<meta property="article:section" content="dev">
<meta property="article:tag" content="linux">
<meta property="article:tag" content="bash">
<meta property="og:image" content="">
<title>tome.one – Bash Magic</title>
<!-- JS files -->
<script src="./theme/tabler/js/tabler.min.js?1692870487" defer></script>
<script src="https://instant.page/5.2.0" type="module"
integrity="sha384-jnZyxPjiipYXnSU0ygqeac2q7CVYMbh84q0uHVRRxEtvFPiQYbXWUorga2aqZJ0z"></script>
<!-- CSS files -->
<link href="./theme/tabler/css/tabler.min.css?1692870487" rel="stylesheet">
<link href="./theme/css/style.css" rel="stylesheet">
<link rel="stylesheet" href="./theme/tipuesearch/tipuesearch.css">
<link rel="stylesheet" type="text/css"
href="./theme/pygments/monokai.css">
<link rel="shortcut icon" href="https://tome.one/theme/img/favicon.ico" type="image/x-icon">
<link rel="icon" href="https://tome.one/theme/img/favicon.ico" type="image/x-icon">
<style>
:root {
--tblr-font-sans-serif: Inter, -apple-system, BlinkMacSystemFont, San Francisco, Segoe UI, Roboto, Helvetica Neue, sans-serif;
}
body {
font-feature-settings: "cv03", "cv04", "cv11";
}
</style>
</head>
<body>
<div class="page">
<!-- Sidebar -->
<aside class="navbar navbar-vertical navbar-left navbar-expand-lg">
<div class="container-fluid">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#sidebar-menu"
aria-controls="sidebar-menu" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<h1 class="navbar-brand navbar-brand-autodark">
<a href="/" class="text-decoration-none">
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-books" width="24"
height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none"
stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<path d="M5 4m0 1a1 1 0 0 1 1 -1h2a1 1 0 0 1 1 1v14a1 1 0 0 1 -1 1h-2a1 1 0 0 1 -1 -1z"/>
<path d="M9 4m0 1a1 1 0 0 1 1 -1h2a1 1 0 0 1 1 1v14a1 1 0 0 1 -1 1h-2a1 1 0 0 1 -1 -1z"/>
<path d="M5 8h4"/>
<path d="M9 16h4"/>
<path d="M13.803 4.56l2.184 -.53c.562 -.135 1.133 .19 1.282 .732l3.695 13.418a1.02 1.02 0 0 1 -.634 1.219l-.133 .041l-2.184 .53c-.562 .135 -1.133 -.19 -1.282 -.732l-3.695 -13.418a1.02 1.02 0 0 1 .634 -1.219l.133 -.041z"/>
<path d="M14 9l4 -1"/>
<path d="M16 16l3.923 -.98"/>
</svg>
tome.one
</a>
</h1>
<div class="collapse navbar-collapse" id="sidebar-menu">
<ul class="navbar-nav pt-lg-3">
<li class="nav-item ">
<a class="nav-link" href="./pages/about.html">
<span class="nav-link-icon d-md-none d-lg-inline-block"><!-- Download SVG icon from http://tabler-icons.io/i/home -->
<svg xmlns="http://www.w3.org/2000/svg"
class="icon icon-tabler icon-tabler-question-mark" width="24" height="24"
viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none"
stroke-linecap="round" stroke-linejoin="round"><path stroke="none"
d="M0 0h24v24H0z"
fill="none"/><path
d="M8 8a3.5 3 0 0 1 3.5 -3h1a3.5 3 0 0 1 3.5 3a3 3 0 0 1 -2 3a3 4 0 0 0 -2 4"/><path
d="M12 19l0 .01"/></svg>
</span>
<span class="nav-link-title">
About
</span>
</a>
</li>
<li class="nav-item ">
<a class="nav-link" href="./pages/conferences.html">
<span class="nav-link-icon d-md-none d-lg-inline-block"><!-- Download SVG icon from http://tabler-icons.io/i/home -->
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-plane"
width="24" height="24" viewBox="0 0 24 24" stroke-width="2"
stroke="currentColor" fill="none" stroke-linecap="round"
stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path
d="M16 10h4a2 2 0 0 1 0 4h-4l-4 7h-3l2 -7h-4l-2 2h-3l2 -4l-2 -4h3l2 2h4l-2 -7h3z"/></svg>
</span>
<span class="nav-link-title">
Conferences
</span>
</a>
</li>
<li class="nav-item ">
<a class="nav-link" href="./pages/links.html">
<span class="nav-link-icon d-md-none d-lg-inline-block"><!-- Download SVG icon from http://tabler-icons.io/i/home -->
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-link"
width="24" height="24" viewBox="0 0 24 24" stroke-width="2"
stroke="currentColor" fill="none" stroke-linecap="round"
stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path
d="M9 15l6 -6"/><path
d="M11 6l.463 -.536a5 5 0 0 1 7.071 7.072l-.534 .464"/><path
d="M13 18l-.397 .534a5.068 5.068 0 0 1 -7.127 0a4.972 4.972 0 0 1 0 -7.071l.524 -.463"/></svg>
</span>
<span class="nav-link-title">
Links
</span>
</a>
</li>
<li class="nav-item ">
<a class="nav-link" href="./pages/projects.html">
<span class="nav-link-icon d-md-none d-lg-inline-block"><!-- Download SVG icon from http://tabler-icons.io/i/home -->
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-tool"
width="24" height="24" viewBox="0 0 24 24" stroke-width="2"
stroke="currentColor" fill="none" stroke-linecap="round"
stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path
d="M7 10h3v-3l-3.5 -3.5a6 6 0 0 1 8 8l6 6a2 2 0 0 1 -3 3l-6 -6a6 6 0 0 1 -8 -8l3.5 3.5"/></svg>
</span>
<span class="nav-link-title">
Projects
</span>
</a>
</li>
<li class="nav-item ">
<a class="nav-link" href="./pages/publications.html">
<span class="nav-link-icon d-md-none d-lg-inline-block"><!-- Download SVG icon from http://tabler-icons.io/i/home -->
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-book"
width="24" height="24" viewBox="0 0 24 24" stroke-width="2"
stroke="currentColor" fill="none" stroke-linecap="round"
stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path
d="M3 19a9 9 0 0 1 9 0a9 9 0 0 1 9 0"/><path
d="M3 6a9 9 0 0 1 9 0a9 9 0 0 1 9 0"/><path d="M3 6l0 13"/><path
d="M12 6l0 13"/><path d="M21 6l0 13"/></svg>
</span>
<span class="nav-link-title">
Publications
</span>
</a>
</li>
<li class="nav-item ">
<a class="nav-link" href="./pages/talks.html">
<span class="nav-link-icon d-md-none d-lg-inline-block"><!-- Download SVG icon from http://tabler-icons.io/i/home -->
<svg xmlns="http://www.w3.org/2000/svg"
class="icon icon-tabler icon-tabler-speakerphone" width="24" height="24"
viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none"
stroke-linecap="round" stroke-linejoin="round"><path stroke="none"
d="M0 0h24v24H0z"
fill="none"/><path
d="M18 8a3 3 0 0 1 0 6"/><path
d="M10 8v11a1 1 0 0 1 -1 1h-1a1 1 0 0 1 -1 -1v-5"/><path
d="M12 8h0l4.524 -3.77a.9 .9 0 0 1 1.476 .692v12.156a.9 .9 0 0 1 -1.476 .692l-4.524 -3.77h-8a1 1 0 0 1 -1 -1v-4a1 1 0 0 1 1 -1h8"/></svg>
</span>
<span class="nav-link-title">
Talks
</span>
</a>
</li>
<li>
<div class="hr-text">
<span>Archives</span>
</div>
</li>
<li class="nav-item ">
<a class="nav-link" href="./archives.html">
<span class="nav-link-icon d-md-none d-lg-inline-block">
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-archive"
width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"
fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none"
d="M0 0h24v24H0z"
fill="none"/><path
d="M3 4m0 2a2 2 0 0 1 2 -2h14a2 2 0 0 1 2 2v0a2 2 0 0 1 -2 2h-14a2 2 0 0 1 -2 -2z"/><path
d="M5 8v10a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-10"/><path d="M10 12l4 0"/></svg>
</span>
<span class="nav-link-title">
Posts
</span>
</a>
</li>
<li class="nav-item ">
<a class="nav-link" href="./categories.html">
<span class="nav-link-icon d-md-none d-lg-inline-block">
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-category"
width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"
fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none"
d="M0 0h24v24H0z"
fill="none"/><path
d="M4 4h6v6h-6z"/><path d="M14 4h6v6h-6z"/><path d="M4 14h6v6h-6z"/><path
d="M17 17m-3 0a3 3 0 1 0 6 0a3 3 0 1 0 -6 0"/></svg>
</span>
<span class="nav-link-title">
Categories
</span>
</a>
</li>
<li class="nav-item ">
<a class="nav-link" href="./tags.html">
<span class="nav-link-icon d-md-none d-lg-inline-block">
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-tag"
width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"
fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none"
d="M0 0h24v24H0z"
fill="none"/><path
d="M7.5 7.5m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0"/><path
d="M3 6v5.172a2 2 0 0 0 .586 1.414l7.71 7.71a2.41 2.41 0 0 0 3.408 0l5.592 -5.592a2.41 2.41 0 0 0 0 -3.408l-7.71 -7.71a2 2 0 0 0 -1.414 -.586h-5.172a3 3 0 0 0 -3 3z"/></svg>
</span>
<span class="nav-link-title">
Tags
</span>
</a>
</li>
</ul>
</div>
</div>
</aside>
<div class="page-wrapper">
<!-- Page header -->
<div class="page-header d-print-none">
<div class="container-narrow">
<div class="row g-2 align-items-center">
<div class="col">
<!-- Page pre-title -->
<div class="page-pretitle">
tome.one
</div>
<h1 class="article-title" id="bash-magic">Bash Magic</h1>
</div>
</div>
</div>
</div>
<div class="container-narrow">
<div class="row ms-auto align-items-center">
<div class="col-auto pt-3 align-items-start">
<a class="text-black px-2 ps-0 text-decoration-none" target="_blank"
href="https://github.com/amietn">
<svg xmlns="http://www.w3.org/2000/svg"
class="icon icon-tabler icon-tabler-brand-github" width="24" height="24"
viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none"
stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<path d="M9 19c-4.3 1.4 -4.3 -2.5 -6 -3m12 5v-3.5c0 -1 .1 -1.4 -.5 -2c2.8 -.3 5.5 -1.4 5.5 -6a4.6 4.6 0 0 0 -1.3 -3.2a4.2 4.2 0 0 0 -.1 -3.2s-1.1 -.3 -3.5 1.3a12.3 12.3 0 0 0 -6.2 0c-2.4 -1.6 -3.5 -1.3 -3.5 -1.3a4.2 4.2 0 0 0 -.1 3.2a4.6 4.6 0 0 0 -1.3 3.2c0 4.6 2.7 5.7 5.5 6c-.6 .6 -.6 1.2 -.5 2v3.5"/>
</svg>
</a>
<a class="text-black px-2 text-decoration-none" target="_blank" href="https://twitter.com/tmlxs">
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-brand-x"
width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"
fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<path d="M4 4l11.733 16h4.267l-11.733 -16z"/>
<path d="M4 20l6.768 -6.768m2.46 -2.46l6.772 -6.772"/>
</svg>
</a>
<a class="text-black px-2 text-decoration-none" target="_blank"
href="https://www.linkedin.com/in/nilsamiet">
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-brand-linkedin"
width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"
fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<path d="M4 4m0 2a2 2 0 0 1 2 -2h12a2 2 0 0 1 2 2v12a2 2 0 0 1 -2 2h-12a2 2 0 0 1 -2 -2z"/>
<path d="M8 11l0 5"/>
<path d="M8 8l0 .01"/>
<path d="M12 16l0 -5"/>
<path d="M16 16v-3a2 2 0 0 0 -4 0"/>
</svg>
</a>
</div>
<div class="col-auto ms-auto pt-3">
<form action="/search" method="get" autocomplete="off" novalidate=""
onsubmit="return (this.elements['q'].value.length > 0)">
<div class="input-icon">
<span class="input-icon-addon">
<!-- Download SVG icon from http://tabler-icons.io/i/search -->
<svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24"
stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path
d="M10 10m-7 0a7 7 0 1 0 14 0a7 7 0 1 0 -14 0"></path><path d="M21 21l-6 -6"></path></svg>
</span>
<input id="searchbox" name="q" type="text" value=""
class="form-control"
placeholder="Search…"
aria-label="Search in website">
</div>
</form>
</div>
</div>
</div>
<!-- Page body -->
<div class="page-body">
<div class="container-narrow">
<div class="article-content">
<div class="article-header align-items-center">
<span>
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-calendar-month" width="24"
height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none"
stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<path d="M4 7a2 2 0 0 1 2 -2h12a2 2 0 0 1 2 2v12a2 2 0 0 1 -2 2h-12a2 2 0 0 1 -2 -2v-12z"/>
<path d="M16 3v4"/>
<path d="M8 3v4"/>
<path d="M4 11h16"/>
<path d="M7 14h.013"/>
<path d="M10.01 14h.005"/>
<path d="M13.01 14h.005"/>
<path d="M16.015 14h.005"/>
<path d="M13.015 17h.005"/>
<path d="M7.01 17h.005"/>
<path d="M10.01 17h.005"/>
</svg>
<span class="text-secondary">
Fri 02 December 2016
</span> •
</span>
<span>
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-user" width="24" height="24"
viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<path d="M8 7a4 4 0 1 0 8 0a4 4 0 0 0 -8 0"/>
<path d="M6 21v-2a4 4 0 0 1 4 -4h4a4 4 0 0 1 4 4v2"/>
</svg>
<span><a href="/pages/about.html">Nils Amiet</a></span>
</span>
<p class="mt-1">
<a class="badge bg-azure text-white"
href="./tag/linux.html">linux</a>
<a class="badge bg-azure text-white"
href="./tag/bash.html">bash</a>
</p>
</div>
<div class="article-content">
<div class="row row-deck row-cards mb-4">
<div class="col-sm-12 col-lg-12">
<div class="card car">
<div class="card-body">
<p>Over the years I have learned many tips and tricks when working with Bash. Some are well known while others are not. Here are a few tricks that I find the most useful.</p>
<h4>Searching history</h4>
<p>So you know you just typed that command but cannot remember what it was. Fine, just type <code><ctrl>-R</code>. Your prompt will then look something like this:</p>
<div class="highlight"><pre><span></span><code>(reverse-i-search)`':
</code></pre></div>
<p>Start typing part of your command and bash will find it for you. Search results will be narrowed down as you keep typing. Cycle forward through search results by hitting <code><ctrl>-R</code> multiple times.</p>
<p>Cycling backwards through history with <code><ctrl>-S</code> requires enabling XON/XOFF flow control with the following command:</p>
<div class="highlight"><pre><span></span><code>stty -ixon
</code></pre></div>
<p>Otherwise your terminal will freeze upon hitting <code><ctrl>-S</code>. Use <code><ctrl>-Q</code> to unfreeze.</p>
<p>Press <code>Enter</code> to execute the search result and use the arrows to edit the command before executing.</p>
<h4>Customizing the prompt</h4>
<p>The prompt is defined by the <code>PS1</code> variable. The default prompt is quite succinct:</p>
<div class="highlight"><pre><span></span><code>PS1='\s-\v\$'
</code></pre></div>
<p>The result looks like this:</p>
<div class="highlight"><pre><span></span><code><span class="nv">bash</span><span class="o">-</span><span class="mf">4.3</span><span class="p">$</span>
</code></pre></div>
<p>It can be improved by displaying useful information such as the date, hostname, username, current working directory and much more. Here is the PS1 I've been using these days:</p>
<div class="highlight"><pre><span></span><code>PS1='┌─[\e[0;32m\t\e[0m]─[\e[0;36m\u\e[0m\e[0m \e[0;33m\w\e[0m]\n└─>\$ '
</code></pre></div>
<p>It looks like that:</p>
<div class="highlight"><pre><span></span><code>┌─<span class="p">[</span><span class="mi">13</span><span class="o">:</span><span class="mi">33</span><span class="o">:</span><span class="mi">37</span><span class="p">]</span>─<span class="p">[</span><span class="nv">john</span><span class="w"> </span>~<span class="o">/</span><span class="nv">git</span><span class="o">/</span><span class="nv">foo</span><span class="o">/</span><span class="nv">bar</span><span class="o">/</span><span class="nv">www</span><span class="p">]</span>
└─<span class="o">></span><span class="p">$</span>
</code></pre></div>
<p>See the Bash manual for more details about Bash variables.</p>
<h4>Moving and Erasing</h4>
<p>Hit <code><ctrl>-A</code> to move the caret to the beginning of the line. <code><ctrl>-E</code> will move to the end of the line.</p>
<p>Use <code><alt>-F</code> to move one word forward and <code><alt>-B</code> to move one word backwards.</p>
<p><code><ctrl>-U</code> will delete everything to the left of the caret while <code><ctrl>-K</code> will delete everything under and to the right of the caret.</p>
<p>Type <code><ctrl>-W</code> to delete the word to the left (until the first space character). Use <code><alt>-Backspace</code> to delete the word to the left until the next special character. This is useful when working with paths and you want to delete until the next <code>/</code> to the left.</p>
<h4>Recalling history</h4>
<p>Recall the last argument of the last command with <code><alt>-_</code> (<alt>-underscore).</p>
<p>Run the previous command with sudo in front:</p>
<div class="highlight"><pre><span></span><code>sudo !!
</code></pre></div>
<h4>Expansion</h4>
<p>Create a backup copy of a file without typing the file path twice:</p>
<div class="highlight"><pre><span></span><code>cp /path/to/file{,.bak}
</code></pre></div>
<h4>Configuring history</h4>
<p>Increase the number of commands stored in the history and save the time and date each command was executed on:</p>
<div class="highlight"><pre><span></span><code>HISTSIZE=100000
HISTFILESIZE=20000
HISTTIMEFORMAT="[%F %T %Z (%z)] "
</code></pre></div>
<p>The history is stored in <code>~/.bash_history</code> and can be displayed with the <code>history</code> command.</p>
<h4>Copy and paste</h4>
<p>Paste the contents of the clipboard with <code><shift>-Insert</code>.</p>
<p>Run a command with the last argument from your clipboard:</p>
<div class="highlight"><pre><span></span><code><span class="gh">#</span> define a function xc()
xc() {
$@ "$(xclip -o)"
}
<span class="gh">#</span> use with
xc my_command # will execute: my_command <clipboard_contents>
</code></pre></div>
<p>How is this more useful than pasting the clipboard contents with <code><shift>-Insert</code> you say? This will save you enclosing the clipboard contents with quotes since the clipboard contents are passed as a single argument to <code>my_command</code>.</p>
<h4>Useful aliases</h4>
<p>Package management:</p>
<div class="highlight"><pre><span></span><code>alias i='sudo -E pacman -S'
alias r='sudo -E pacman -Rs'
alias s='pacman -Ss'
alias syu='sudo -E pacman -Syu'
</code></pre></div>
<p>Listing files and grepping:</p>
<div class="highlight"><pre><span></span><code>alias ls='ls --color=auto --group-directories-first -lh'
alias lsgrep='ls | grep -i'
</code></pre></div>
<p>Systemd:</p>
<div class="highlight"><pre><span></span><code>alias sc='sudo -E systemctl'
</code></pre></div>
<p>Systemd will be the topic of another post. Enjoy!</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>