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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Bash Golf Part 4</title>
<link rel="shortcut icon" type="image/gif" href="/favicon.ico" />
<link rel="stylesheet" href="../style.css" />
<link rel="stylesheet" href="style-override.css" />
</head>
<body>
<p class="header">
<a href="https://foo.zone">Home</a> | <a href="https://codeberg.org/snonux/foo.zone/src/branch/content-md/gemfeed/2025-09-14-bash-golf-part-4.md">Markdown</a> | <a href="gemini://foo.zone/gemfeed/2025-09-14-bash-golf-part-4.gmi">Gemini</a>
</p>
<h1 style='display: inline' id='bash-golf-part-4'>Bash Golf Part 4</h1><br />
<br />
<span class='quote'>Published at 2025-09-13T12:04:03+03:00</span><br />
<br />
<span>This is the fourth blog post about my Bash Golf series. This series is random Bash tips, tricks, and weirdnesses I have encountered over time. </span><br />
<br />
<a class='textlink' href='./2021-11-29-bash-golf-part-1.html'>2021-11-29 Bash Golf Part 1</a><br />
<a class='textlink' href='./2022-01-01-bash-golf-part-2.html'>2022-01-01 Bash Golf Part 2</a><br />
<a class='textlink' href='./2023-12-10-bash-golf-part-3.html'>2023-12-10 Bash Golf Part 3</a><br />
<a class='textlink' href='./2025-09-14-bash-golf-part-4.html'>2025-09-14 Bash Golf Part 4 (You are currently reading this)</a><br />
<br />
<pre>
'\ '\ '\ '\ . . |>18>>
\ \ \ \ . ' . |
O>> O>> O>> O>> . 'o |
\ .\. .. .\. .. .\. .. . |
/\ . /\ . /\ . /\ . . |
/ / . / / .'. / / .'. / / .' . |
jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Art by Joan Stark, mod. by Paul Buetow
</pre>
<br />
<h2 style='display: inline' id='table-of-contents'>Table of Contents</h2><br />
<br />
<ul>
<li><a href='#bash-golf-part-4'>Bash Golf Part 4</a></li>
<li>⇢ <a href='#split-pipelines-with-tee--process-substitution'>Split pipelines with tee + process substitution</a></li>
<li>⇢ <a href='#heredocs-for-remote-sessions-and-their-gotchas'>Heredocs for remote sessions (and their gotchas)</a></li>
<li>⇢ <a href='#namespacing-and-dynamic-dispatch-with-'>Namespacing and dynamic dispatch with <span class='inlinecode'>::</span></a></li>
<li>⇢ <a href='#indirect-references-with-namerefs'>Indirect references with namerefs</a></li>
<li>⇢ <a href='#function-declaration-forms'>Function declaration forms</a></li>
<li>⇢ <a href='#chaining-function-calls-in-conditionals'>Chaining function calls in conditionals</a></li>
<li>⇢ <a href='#grep-sed-awk-quickies'>Grep, sed, awk quickies</a></li>
<li>⇢ <a href='#safe-xargs-with-nuls'>Safe xargs with NULs</a></li>
<li>⇢ <a href='#efficient-file-to-variable-and-arrays'>Efficient file-to-variable and arrays</a></li>
<li>⇢ <a href='#quick-password-generator'>Quick password generator</a></li>
<li>⇢ <a href='#yes-for-automation'><span class='inlinecode'>yes</span> for automation</a></li>
<li>⇢ <a href='#forcing-true-to-fail-and-vice-versa'>Forcing <span class='inlinecode'>true</span> to fail (and vice versa)</a></li>
<li>⇢ <a href='#restricted-bash'>Restricted Bash</a></li>
<li>⇢ <a href='#useless-use-of-cat-and-when-its-ok'>Useless use of cat (and when it’s ok)</a></li>
<li>⇢ <a href='#atomic-locking-with-mkdir'>Atomic locking with <span class='inlinecode'>mkdir</span></a></li>
<li>⇢ <a href='#smarter-globs-and-faster-find-exec'>Smarter globs and faster find-exec</a></li>
</ul><br />
<h2 style='display: inline' id='split-pipelines-with-tee--process-substitution'>Split pipelines with tee + process substitution</h2><br />
<br />
<span>Sometimes you want to fan out one stream to multiple consumers and still continue the original pipeline. <span class='inlinecode'>tee</span> plus process substitution does exactly that:</span><br />
<br />
<pre>
somecommand \
| tee >(command1) >(command2) \
| command3
</pre>
<br />
<span>All of <span class='inlinecode'>command1</span>, <span class='inlinecode'>command2</span>, and <span class='inlinecode'>command3</span> see the output of <span class='inlinecode'>somecommand</span>. Example:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><b><u><font color="#000000">printf</font></u></b> <font color="#808080">'a</font>\n<font color="#808080">b</font>\n<font color="#808080">'</font> \
| tee >(sed <font color="#808080">'s/.*/X:&/; s/$/ :c1/'</font>) >(tr a-z A-Z | sed <font color="#808080">'s/$/ :c2/'</font>) \
| sed <font color="#808080">'s/$/ :c3/'</font>
</pre>
<br />
<span>Output:</span><br />
<br />
<pre>
a :c3
b :c3
A :c2 :c3
B :c2 :c3
X:a :c1 :c3
X:b :c1 :c3
</pre>
<br />
<span>This relies on Bash process substitution (<span class='inlinecode'>>(...)</span>). Make sure your shell is Bash and not a POSIX <span class='inlinecode'>/bin/sh</span>.</span><br />
<br />
<span>Example (fails under <span class='inlinecode'>dash</span>/POSIX sh):</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>/bin/sh -c <font color="#808080">'echo hi | tee >(cat)'</font>
<i><font color="silver"># /bin/sh: 1: Syntax error: "(" unexpected</font></i>
</pre>
<br />
<span>Combine with <span class='inlinecode'>set -o pipefail</span> if failures in side branches should fail the whole pipeline.</span><br />
<br />
<span>Example:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><b><u><font color="#000000">set</font></u></b> -o pipefail
<b><u><font color="#000000">printf</font></u></b> <font color="#808080">'ok</font>\n<font color="#808080">'</font> | tee >(<b><u><font color="#000000">false</font></u></b>) | cat >/dev/null
echo $? <i><font color="silver"># 1 because a side branch failed</font></i>
</pre>
<br />
<span>Further reading:</span><br />
<br />
<a class='textlink' href='https://blogtitle.github.io/splitting-pipelines/'>Splitting pipelines with tee</a><br />
<br />
<h2 style='display: inline' id='heredocs-for-remote-sessions-and-their-gotchas'>Heredocs for remote sessions (and their gotchas)</h2><br />
<br />
<span>Heredocs are great to send multiple commands over SSH in a readable way:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>ssh <font color="#808080">"$SSH_USER@$SSH_HOST"</font> <<EOF
<i><font color="silver"># Go to the work directory</font></i>
cd <font color="#808080">"$WORK_DIR"</font>
<i><font color="silver"># Make a git pull</font></i>
git pull
<i><font color="silver"># Export environment variables required for the service to run</font></i>
<b><u><font color="#000000">export</font></u></b> AUTH_TOKEN=<font color="#808080">"$APP_AUTH_TOKEN"</font>
<i><font color="silver"># Start the service</font></i>
docker compose up -d --build
EOF
</pre>
<br />
<span>Tips:</span><br />
<br />
<span>Quoting the delimiter changes interpolation. Use <span class='inlinecode'><<'EOF'</span> to avoid local expansion and send the content literally.</span><br />
<br />
<span>Example:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>FOO=bar
cat <<<font color="#808080">'EOF'</font>
$FOO is not expanded here
EOF
</pre>
<br />
<span>Prefer explicit quoting for variables (as above) to avoid surprises. Example (spaces preserved only when quoted):</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>WORK_DIR=<font color="#808080">"/tmp/my work"</font>
ssh host <<EOF
cd $WORK_DIR <i><font color="silver"># may break if unquoted</font></i>
cd <font color="#808080">"$WORK_DIR"</font> <i><font color="silver"># safe</font></i>
EOF
</pre>
<br />
<span>Consider <span class='inlinecode'>set -euo pipefail</span> at the top of the remote block for stricter error handling. Example:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>ssh host <<<font color="#808080">'EOF'</font>
<b><u><font color="#000000">set</font></u></b> -euo pipefail
<b><u><font color="#000000">false</font></u></b> <i><font color="silver"># causes immediate failure</font></i>
echo never
EOF
</pre>
<br />
<span>Indent-friendly variant: use a dash to strip leading tabs in the body:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>cat <<-EOF > script.sh
<i><font color="silver">#!/usr/bin/env bash</font></i>
echo <font color="#808080">"tab-indented content is dedented"</font>
EOF
</pre>
<br />
<span>Further reading:</span><br />
<br />
<a class='textlink' href='https://rednafi.com/misc/heredoc_headache/'>Heredoc headaches and fixes</a><br />
<br />
<h2 style='display: inline' id='namespacing-and-dynamic-dispatch-with-'>Namespacing and dynamic dispatch with <span class='inlinecode'>::</span></h2><br />
<br />
<span>You can emulate simple namespacing by encoding hierarchy in function names. One neat pattern is pseudo-inheritance via a tiny <span class='inlinecode'>super</span> helper that maps <span class='inlinecode'>pkg::lang::action</span> to a <span class='inlinecode'>pkg::base::action</span> default.</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><i><font color="silver">#!/usr/bin/env bash</font></i>
<b><u><font color="#000000">set</font></u></b> -euo pipefail
super() {
<b><u><font color="#000000">local</font></u></b> -r fn=${FUNCNAME[1]}
<i><font color="silver"># Split name on :: and dispatch to base implementation</font></i>
<b><u><font color="#000000">local</font></u></b> -a parts=( ${fn//::/ } )
<font color="#808080">"${parts[0]}::base::${parts[2]}"</font> <font color="#808080">"$@"</font>
}
foo::base::greet() { echo <font color="#808080">"base: $@"</font>; }
foo::german::greet() { super <font color="#808080">"Guten Tag, $@!"</font>; }
foo::english::greet() { super <font color="#808080">"Good day, $@!"</font>; }
<b><u><font color="#000000">for</font></u></b> lang <b><u><font color="#000000">in</font></u></b> german english; <b><u><font color="#000000">do</font></u></b>
foo::$lang::greet Paul
<b><u><font color="#000000">done</font></u></b>
</pre>
<br />
<span>Output:</span><br />
<br />
<pre>
base: Guten Tag, Paul!
base: Good day, Paul!
</pre>
<br />
<h2 style='display: inline' id='indirect-references-with-namerefs'>Indirect references with namerefs</h2><br />
<br />
<span><span class='inlinecode'>declare -n</span> creates a name reference — a variable that points to another variable. It’s cleaner than <span class='inlinecode'>eval</span> for indirection:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>user_name=paul
<b><u><font color="#000000">declare</font></u></b> -n ref=user_name
echo <font color="#808080">"$ref"</font> <i><font color="silver"># paul</font></i>
ref=julia
echo <font color="#808080">"$user_name"</font> <i><font color="silver"># julia</font></i>
</pre>
<br />
<span>Output:</span><br />
<br />
<pre>
paul
julia
</pre>
<br />
<span>Namerefs are local to functions when declared with <span class='inlinecode'>local -n</span>. Requires Bash ≥4.3.</span><br />
<br />
<span>You can also construct the target name dynamically:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>make_var() {
<b><u><font color="#000000">local</font></u></b> idx=$1; <b><u><font color="#000000">shift</font></u></b>
<b><u><font color="#000000">local</font></u></b> name=<font color="#808080">"slot_$idx"</font>
<b><u><font color="#000000">printf</font></u></b> -v <font color="#808080">"$name"</font> <font color="#808080">'%s'</font> <font color="#808080">"$*"</font> <i><font color="silver"># create variable slot_$idx</font></i>
}
get_var() {
<b><u><font color="#000000">local</font></u></b> idx=$1
<b><u><font color="#000000">local</font></u></b> -n ref=<font color="#808080">"slot_$idx"</font> <i><font color="silver"># bind ref to slot_$idx</font></i>
<b><u><font color="#000000">printf</font></u></b> <font color="#808080">'%s</font>\n<font color="#808080">'</font> <font color="#808080">"$ref"</font>
}
make_var <font color="#000000">7</font> <font color="#808080">"seven"</font>
get_var <font color="#000000">7</font>
</pre>
<br />
<span>Output:</span><br />
<br />
<pre>
seven
</pre>
<br />
<h2 style='display: inline' id='function-declaration-forms'>Function declaration forms</h2><br />
<br />
<span>All of these work in Bash, but only the first one is POSIX-ish:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>foo() { echo foo; }
function foo { echo foo; }
function foo() { echo foo; }
</pre>
<br />
<span>Recommendation: prefer <span class='inlinecode'>name() { ... }</span> for portability and consistency.</span><br />
<br />
<h2 style='display: inline' id='chaining-function-calls-in-conditionals'>Chaining function calls in conditionals</h2><br />
<br />
<span>Functions return a status like commands. You can short-circuit them in conditionals:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>deploy_check() { <b><u><font color="#000000">test</font></u></b> -f deploy.yaml; }
smoke_test() { curl -fsS http://localhost/healthz >/dev/null; }
<b><u><font color="#000000">if</font></u></b> deploy_check || smoke_test; <b><u><font color="#000000">then</font></u></b>
echo <font color="#808080">"All good."</font>
<b><u><font color="#000000">else</font></u></b>
echo <font color="#808080">"Something failed."</font> >&<font color="#000000">2</font>
<b><u><font color="#000000">fi</font></u></b>
</pre>
<br />
<span>You can also compress it golf-style:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>deploy_check || smoke_test && echo ok || echo fail >&<font color="#000000">2</font>
</pre>
<br />
<h2 style='display: inline' id='grep-sed-awk-quickies'>Grep, sed, awk quickies</h2><br />
<br />
<span>Word match and context: <span class='inlinecode'>grep -w word file</span>; with context: <span class='inlinecode'>grep -C3 foo file</span> (same as <span class='inlinecode'>-A3 -B3</span>). Example:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>cat > /tmp/ctx.txt <<EOF
one
foo
two
three
bar
EOF
grep -C<font color="#000000">1</font> foo /tmp/ctx.txt
</pre>
<br />
<span>Output:</span><br />
<br />
<pre>
one
foo
two
</pre>
<br />
<span>Skip a directory while recursing: <span class='inlinecode'>grep -R --exclude-dir=foo 'bar' /path</span>. Example:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>mkdir -p /tmp/golf/foo /tmp/golf/src
<b><u><font color="#000000">printf</font></u></b> <font color="#808080">'bar</font>\n<font color="#808080">'</font> > /tmp/golf/src/a.txt
<b><u><font color="#000000">printf</font></u></b> <font color="#808080">'bar</font>\n<font color="#808080">'</font> > /tmp/golf/foo/skip.txt
grep -R --exclude-dir=foo <font color="#808080">'bar'</font> /tmp/golf
</pre>
<br />
<span>Output:</span><br />
<br />
<pre>
/tmp/golf/src/a.txt:bar
</pre>
<br />
<span>Insert lines with sed: <span class='inlinecode'>sed -e '1isomething' -e '3isomething' file</span>. Example:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><b><u><font color="#000000">printf</font></u></b> <font color="#808080">'A</font>\n<font color="#808080">B</font>\n<font color="#808080">C</font>\n<font color="#808080">'</font> > /tmp/s.txt
sed -e <font color="#808080">'1iHEAD'</font> -e <font color="#808080">'3iMID'</font> /tmp/s.txt
</pre>
<br />
<span>Output:</span><br />
<br />
<pre>
HEAD
A
B
MID
C
</pre>
<br />
<span>Drop last column with awk: <span class='inlinecode'>awk 'NF{NF-=1};1' file</span>. Example:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><b><u><font color="#000000">printf</font></u></b> <font color="#808080">'a b c</font>\n<font color="#808080">x y z</font>\n<font color="#808080">'</font> > /tmp/t.txt
cat /tmp/t.txt
echo
awk <font color="#808080">'NF{NF-=1};1'</font> /tmp/t.txt
</pre>
<br />
<span>Output:</span><br />
<br />
<pre>
a b c
x y z
a b
x y
</pre>
<br />
<h2 style='display: inline' id='safe-xargs-with-nuls'>Safe xargs with NULs</h2><br />
<br />
<span>Avoid breaking on spaces/newlines by pairing <span class='inlinecode'>find -print0</span> with <span class='inlinecode'>xargs -0</span>:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>find . -type f -name <font color="#808080">'*.log'</font> -print<font color="#000000">0</font> | xargs -<font color="#000000">0</font> rm -f
</pre>
<br />
<span>Example with spaces and NULs only:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><b><u><font color="#000000">printf</font></u></b> <font color="#808080">'a</font>\0<font color="#808080">b c</font>\0<font color="#808080">'</font> | xargs -<font color="#000000">0</font> -I{} <b><u><font color="#000000">printf</font></u></b> <font color="#808080">'<%s></font>\n<font color="#808080">'</font> {}
</pre>
<br />
<span>Output:</span><br />
<span> </span><br />
<pre>
<a>
<b c>
</pre>
<br />
<h2 style='display: inline' id='efficient-file-to-variable-and-arrays'>Efficient file-to-variable and arrays</h2><br />
<br />
<span>Read a whole file into a variable without spawning <span class='inlinecode'>cat</span>:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>cfg=$(<config.ini)
</pre>
<br />
<span>Read lines into an array safely with <span class='inlinecode'>mapfile</span> (aka <span class='inlinecode'>readarray</span>):</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>mapfile -t lines < <(grep -v <font color="#808080">'^#'</font> config.ini)
<b><u><font color="#000000">printf</font></u></b> <font color="#808080">'%s</font>\n<font color="#808080">'</font> <font color="#808080">"${lines[@]}"</font>
</pre>
<br />
<span>Assign formatted strings without a subshell using <span class='inlinecode'>printf -v</span>:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><b><u><font color="#000000">printf</font></u></b> -v msg <font color="#808080">'Hello %s, id=%04d'</font> <font color="#808080">"$USER"</font> <font color="#000000">42</font>
echo <font color="#808080">"$msg"</font>
</pre>
<br />
<span>Output:</span><br />
<br />
<pre>
Hello paul, id=0042
</pre>
<br />
<span>Read NUL-delimited data (pairs well with <span class='inlinecode'>-print0</span>):</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>mapfile -d <font color="#808080">''</font> -t files < <(find . -type f -print<font color="#000000">0</font>)
<b><u><font color="#000000">printf</font></u></b> <font color="#808080">'%s</font>\n<font color="#808080">'</font> <font color="#808080">"${files[@]}"</font>
</pre>
<br />
<h2 style='display: inline' id='quick-password-generator'>Quick password generator</h2><br />
<br />
<span>Pure Bash with <span class='inlinecode'>/dev/urandom</span>:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>LC_ALL=C tr -dc <font color="#808080">'A-Za-z0-9_'</font> </dev/urandom | head -c <font color="#000000">16</font>; echo
</pre>
<br />
<span>Alternative using <span class='inlinecode'>openssl</span>:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>openssl rand -base<font color="#000000">64</font> <font color="#000000">16</font> | tr -d <font color="#808080">'</font>\n<font color="#808080">'</font> | cut -c<font color="#000000">1</font>-<font color="#000000">22</font>
</pre>
<br />
<h2 style='display: inline' id='yes-for-automation'><span class='inlinecode'>yes</span> for automation</h2><br />
<br />
<span><span class='inlinecode'>yes</span> streams a string repeatedly; handy for feeding interactive commands or quick load generation:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>yes | rm -r large_directory <i><font color="silver"># auto-confirm</font></i>
yes n | dangerous-command <i><font color="silver"># auto-decline</font></i>
yes anything | head -n<font color="#000000">1</font> <i><font color="silver"># prints one line: anything</font></i>
</pre>
<br />
<h2 style='display: inline' id='forcing-true-to-fail-and-vice-versa'>Forcing <span class='inlinecode'>true</span> to fail (and vice versa)</h2><br />
<br />
<span>You can shadow builtins with functions:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>true() { <b><u><font color="#000000">return</font></u></b> <font color="#000000">1</font>; }
false() { <b><u><font color="#000000">return</font></u></b> <font color="#000000">0</font>; }
<b><u><font color="#000000">true</font></u></b> || echo <font color="#808080">'true failed'</font>
<b><u><font color="#000000">false</font></u></b> && echo <font color="#808080">'false succeeded'</font>
<i><font color="silver"># Bypass function with builtin/command</font></i>
<b><u><font color="#000000">builtin</font></u></b> <b><u><font color="#000000">true</font></u></b> <i><font color="silver"># returns 0</font></i>
<b><u><font color="#000000">command</font></u></b> <b><u><font color="#000000">true</font></u></b> <i><font color="silver"># returns 0</font></i>
</pre>
<br />
<span>To disable a builtin entirely: <span class='inlinecode'>enable -n true</span> (re-enable with <span class='inlinecode'>enable true</span>).</span><br />
<br />
<span>Further reading:</span><br />
<br />
<a class='textlink' href='https://blog.robertelder.org/force-true-command-to-return-false/'>Force true to return false</a><br />
<br />
<h2 style='display: inline' id='restricted-bash'>Restricted Bash</h2><br />
<br />
<span><span class='inlinecode'>bash -r</span> (or <span class='inlinecode'>rbash</span>) starts a restricted shell that limits potentially dangerous actions, for example:</span><br />
<br />
<ul>
<li>Changing directories (<span class='inlinecode'>cd</span>).</li>
<li>Modifying <span class='inlinecode'>PATH</span>, <span class='inlinecode'>SHELL</span>, <span class='inlinecode'>BASH_ENV</span>, or <span class='inlinecode'>ENV</span>.</li>
<li>Redirecting output.</li>
<li>Running commands with <span class='inlinecode'>/</span> in the name.</li>
<li>Using <span class='inlinecode'>exec</span>.</li>
</ul><br />
<span>It’s a coarse sandbox for highly constrained shells; read <span class='inlinecode'>man bash</span> (RESTRICTED SHELL) for details and caveats.</span><br />
<br />
<span>Example session:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>rbash -c <font color="#808080">'cd /'</font> <i><font color="silver"># cd: restricted</font></i>
rbash -c <font color="#808080">'PATH=/tmp'</font> <i><font color="silver"># PATH: restricted</font></i>
rbash -c <font color="#808080">'echo hi > out'</font> <i><font color="silver"># redirection: restricted</font></i>
rbash -c <font color="#808080">'/bin/echo hi'</font> <i><font color="silver"># commands with /: restricted</font></i>
rbash -c <font color="#808080">'exec ls'</font> <i><font color="silver"># exec: restricted</font></i>
</pre>
<br />
<h2 style='display: inline' id='useless-use-of-cat-and-when-its-ok'>Useless use of cat (and when it’s ok)</h2><br />
<br />
<span>Avoid the extra process if a command already reads files or <span class='inlinecode'>STDIN</span>:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><i><font color="silver"># Prefer</font></i>
grep -i foo file
<file grep -i foo <i><font color="silver"># or feed via redirection</font></i>
<i><font color="silver"># Over</font></i>
cat file | grep -i foo
</pre>
<br />
<span>But for interactive composition, or when you truly need to concatenate multiple sources into a single stream, <span class='inlinecode'>cat</span> is fine, as you may think, "First I need the content, then I do X." Changing the "useless use of cat" in retrospect is really a waste of time for one-time interactive use:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>cat file1 file2 | grep -i foo
</pre>
<br />
<span>From notes: “Good for interactivity; Useless use of cat” — use judgment.</span><br />
<br />
<h2 style='display: inline' id='atomic-locking-with-mkdir'>Atomic locking with <span class='inlinecode'>mkdir</span></h2><br />
<br />
<span>Portable advisory locks can be emulated with <span class='inlinecode'>mkdir</span> because it’s atomic:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>lockdir=/tmp/myjob.lock
<b><u><font color="#000000">if</font></u></b> mkdir <font color="#808080">"$lockdir"</font> <font color="#000000">2</font>>/dev/null; <b><u><font color="#000000">then</font></u></b>
<b><u><font color="#000000">trap</font></u></b> <font color="#808080">'rmdir "$lockdir"'</font> EXIT INT TERM
<i><font color="silver"># critical section</font></i>
do_work
<b><u><font color="#000000">else</font></u></b>
echo <font color="#808080">"Another instance is running"</font> >&<font color="#000000">2</font>
<b><u><font color="#000000">exit</font></u></b> <font color="#000000">1</font>
<b><u><font color="#000000">fi</font></u></b>
</pre>
<br />
<span>This works well on Linux. Remove the lock in <span class='inlinecode'>trap</span> so crashes don’t leave stale locks.</span><br />
<br />
<h2 style='display: inline' id='smarter-globs-and-faster-find-exec'>Smarter globs and faster find-exec</h2><br />
<br />
<ul>
<li>Enable extended globs when useful: <span class='inlinecode'>shopt -s extglob</span>; then patterns like <span class='inlinecode'>!(tmp|cache)</span> work.</li>
<li>Use <span class='inlinecode'>-exec ... {} +</span> to batch many paths in fewer process invocations:</li>
</ul><br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>find . -name <font color="#808080">'*.log'</font> -exec gzip -<font color="#000000">9</font> {} +
</pre>
<br />
<span>Example for extglob (exclude two dirs from listing):</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><b><u><font color="#000000">shopt</font></u></b> -s extglob
ls -d -- !(.git|node_modules) <font color="#000000">2</font>>/dev/null
</pre>
<br />
<span>E-Mail your comments to <span class='inlinecode'>paul@nospam.buetow.org</span> :-)</span><br />
<br />
<span>Other related posts are:</span><br />
<br />
<a class='textlink' href='./2025-09-14-bash-golf-part-4.html'>2025-09-14 Bash Golf Part 4 (You are currently reading this)</a><br />
<a class='textlink' href='./2023-12-10-bash-golf-part-3.html'>2023-12-10 Bash Golf Part 3</a><br />
<a class='textlink' href='./2022-01-01-bash-golf-part-2.html'>2022-01-01 Bash Golf Part 2</a><br />
<a class='textlink' href='./2021-11-29-bash-golf-part-1.html'>2021-11-29 Bash Golf Part 1</a><br />
<a class='textlink' href='./2021-06-05-gemtexter-one-bash-script-to-rule-it-all.html'>2021-06-05 Gemtexter - One Bash script to rule it all</a><br />
<a class='textlink' href='./2021-05-16-personal-bash-coding-style-guide.html'>2021-05-16 Personal Bash coding style guide</a><br />
<br />
<a class='textlink' href='../'>Back to the main site</a><br />
<p class="footer">
Generated with <a href="https://codeberg.org/snonux/gemtexter">Gemtexter 3.0.1-develop</a> |
served by <a href="https://www.OpenBSD.org">OpenBSD</a>/<a href="https://man.openbsd.org/relayd.8">relayd(8)</a>+<a href="https://man.openbsd.org/httpd.8">httpd(8)</a> |
<a href="https://foo.zone/site-mirrors.html">Site Mirrors</a>
<br />
Webring: <a href="https://shring.sh/foo.zone/previous">previous</a> | <a href="https://shring.sh">shring</a> | <a href="https://shring.sh/foo.zone/next">next</a>
</p>
</body>
</html>
|