summaryrefslogtreecommitdiff
path: root/gemfeed/2021-05-16-personal-bash-coding-style-guide.html
blob: 2511cec9768023d5f4cf6e9da39f7da6d5773702 (plain)
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
<!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" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Personal Bash coding style guide</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>
<div class="rfx-overlay-grid"></div>
<div class="rfx-overlay-scanlines"></div>
<div id="rfx-stars"></div>
<div class="rfx-vignette"></div>
<p class="header">
<a href="https://foo.zone">Home</a> | <a href="https://codeberg.org/snonux/foo.zone/src/branch/content-md/gemfeed/2021-05-16-personal-bash-coding-style-guide.md">Markdown</a> | <a href="gemini://foo.zone/gemfeed/2021-05-16-personal-bash-coding-style-guide.gmi">Gemini</a>
</p>
<h1 style='display: inline' id='personal-bash-coding-style-guide'>Personal Bash coding style guide</h1><br />
<br />
<span class='quote'>Published at 2021-05-16T14:51:57+01:00</span><br />
<br />
<span>Lately, I have been polishing and writing a lot of Bash code. Not that I never wrote a lot of Bash, but now as I also looked through the Google Shell Style Guide, I thought it is time also to write my thoughts on that. I agree with that guide in most, but not in all points. </span><br />
<br />
<a class='textlink' href='https://google.github.io/styleguide/shellguide.html'>Google Shell Style Guide</a><br />
<br />
<pre>
   .---------------------------.
  /,--..---..---..---..---..--. `.
 //___||___||___||___||___||___\_|
 [j__ ######################## [_|
    \============================|
 .==|  |"""||"""||"""||"""| |"""||
/======"---""---""---""---"=|  =||
|____    []*          ____  | ==||
//  \\               //  \\ |===||  hjw
"\__/"---------------"\__/"-+---+&#39;
</pre>
<br />
<h2 style='display: inline' id='table-of-contents'>Table of Contents</h2><br />
<br />
<ul>
<li><a href='#personal-bash-coding-style-guide'>Personal Bash coding style guide</a></li>
<li>⇢ <a href='#my-modifications'>My modifications</a></li>
<li>⇢ ⇢ <a href='#shebang'>Shebang</a></li>
<li>⇢ ⇢ <a href='#two-space-soft-tabs-indentation'>Two space soft-tabs indentation</a></li>
<li>⇢ ⇢ <a href='#breaking-long-pipes'>Breaking long pipes</a></li>
<li>⇢ ⇢ <a href='#quoting-your-variables'>Quoting your variables</a></li>
<li>⇢ ⇢ <a href='#prefer-built-in-commands-over-external-commands'>Prefer built-in commands over external commands</a></li>
<li>⇢ <a href='#my-additions'>My additions</a></li>
<li>⇢ ⇢ <a href='#use-of--yes--and--no-'>Use of &#39;yes&#39; and &#39;no&#39;</a></li>
<li>⇢ ⇢ <a href='#non-evil-alternative-to-variable-assignments-via-eval'>Non-evil alternative to variable assignments via eval</a></li>
<li>⇢ ⇢ <a href='#prefer-pipes-over-arrays-for-list-processing'>Prefer pipes over arrays for list processing</a></li>
<li>⇢ ⇢ <a href='#assign-then-shift'>Assign-then-shift</a></li>
<li>⇢ ⇢ <a href='#paranoid-mode'>Paranoid mode</a></li>
<li>⇢ <a href='#learned'>Learned</a></li>
<li>⇢ ⇢ <a href='#unintended-lexicographical-comparison'>Unintended lexicographical comparison.</a></li>
<li>⇢ ⇢ <a href='#pipestatus'>PIPESTATUS</a></li>
<li>⇢ <a href='#use-common-sense-and-be-consistent'>Use common sense and BE CONSISTENT.</a></li>
<li>⇢ <a href='#advanced-bash-learning-pro-tip'>Advanced Bash learning pro tip</a></li>
</ul><br />
<h2 style='display: inline' id='my-modifications'>My modifications</h2><br />
<br />
<span>These are my modifications to the Google Guide.</span><br />
<br />
<h3 style='display: inline' id='shebang'>Shebang</h3><br />
<br />
<span>Google recommends using always...</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="#ababab">#!/bin/bash </font></i>
</pre>
<br />
<span>... as the shebang line, but that does not work on all Unix and Unix-like operating systems (e.g., the *BSDs don&#39;t have Bash installed to /bin/bash). Better is:</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="#ababab">#!/usr/bin/env bash</font></i>
</pre>
<br />
<h3 style='display: inline' id='two-space-soft-tabs-indentation'>Two space soft-tabs indentation</h3><br />
<br />
<span>I know there have been many tab- and soft-tab wars on this planet. Google recommends using two space soft-tabs for Bash scripts. </span><br />
<br />
<span>I don&#39;t care if I use two or four space indentations. I agree, however, that we should not use tabs. I tend to use four-space soft-tabs as that&#39;s how I currently configured Vim for any programming language. What matters most, though, is consistency within the same script/project.</span><br />
<br />
<span>Google also recommends limiting the line length to 80 characters. For some people, that seems to be an old habit from the &#39;80s, where all computer terminals couldn&#39;t display longer lines. But I think that the 80 character mark is still a good practice, at least for shell scripts. For example, I am often writing code on a Microsoft Go Tablet PC (running Linux, of course), and it comes in convenient if the lines are not too long due to the relatively small display on the device.</span><br />
<br />
<span>I hit the 80 character line length quicker with the four spaces than with two spaces, but that makes me refactor the Bash code more aggressively, which is a good thing. </span><br />
<br />
<h3 style='display: inline' id='breaking-long-pipes'>Breaking long pipes</h3><br />
<br />
<span>Google recommends breaking up long pipes like this:</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="#ababab"># All fits on one line</font></i>
<font color="#ff0000">command1 </font><font color="#F3E651">|</font><font color="#ff0000"> command2</font>

<i><font color="#ababab"># Long commands</font></i>
<font color="#ff0000">command1 </font><font color="#F3E651">\</font>
<font color="#ff0000">  </font><font color="#F3E651">|</font><font color="#ff0000"> command2 </font><font color="#F3E651">\</font>
<font color="#ff0000">  </font><font color="#F3E651">|</font><font color="#ff0000"> command3 </font><font color="#F3E651">\</font>
<font color="#ff0000">  </font><font color="#F3E651">|</font><font color="#ff0000"> command4</font>
</pre>
<br />
<span>I think there is a better way like the following, which is less noisy. The pipe | already indicates the Bash that another command is expected, thus making the explicit line breaks with \ obsolete:</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="#ababab"># Long commands</font></i>
<font color="#ff0000">command1 </font><font color="#F3E651">|</font>
<font color="#ff0000">    command2 </font><font color="#F3E651">|</font>
<font color="#ff0000">    command3 </font><font color="#F3E651">|</font>
<font color="#ff0000">    command4</font>
</pre>
<br />
<span class='quote'>Update: It&#39;s 2023 now, and I have changed my mind. I think Google&#39;s way is the better one. It may be a bit more to type, but the leading <span class='inlinecode'>|</span> are a nice eye catcher, so you know immediately what is going on!</span><br />
<br />
<h3 style='display: inline' id='quoting-your-variables'>Quoting your variables</h3><br />
<br />
<span>Google recommends always quote your variables. Generally, it would be best if you did that only for variables where you are unsure about the content/values of the variables (e.g., content is from an external input source and may contain whitespace or other special characters). In my opinion, the code will become quite noisy when you always quote your variables like this:</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><font color="#7bc710">greet ()</font><font color="#ff0000"> {</font>
<font color="#ff0000">    </font><b><font color="#ffffff">local</font></b><font color="#ff0000"> -r </font><font color="#ff0000">greeting</font><font color="#F3E651">=</font><font color="#bb00ff">"${1}"</font>
<font color="#ff0000">    </font><b><font color="#ffffff">local</font></b><font color="#ff0000"> -r </font><font color="#ff0000">name</font><font color="#F3E651">=</font><font color="#bb00ff">"${2}"</font>
<font color="#ff0000">    echo </font><font color="#bb00ff">"${greeting} ${name}!"</font>
<font color="#ff0000">}</font>
</pre>
<br />
<span>In this particular example, I agree that you should quote them as you don&#39;t know the input (are there, for example, whitespace characters?). But if you are sure that you are only using simple bare words, then I think that the code looks much cleaner when you do this instead:</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><font color="#7bc710">say_hello_to_paul ()</font><font color="#ff0000"> {</font>
<font color="#ff0000">    </font><b><font color="#ffffff">local</font></b><font color="#ff0000"> -r </font><font color="#ff0000">greeting</font><font color="#F3E651">=</font><font color="#ff0000">Hello</font>
<font color="#ff0000">    </font><b><font color="#ffffff">local</font></b><font color="#ff0000"> -r </font><font color="#ff0000">name</font><font color="#F3E651">=</font><font color="#ff0000">Paul</font>
<font color="#ff0000">    echo </font><font color="#bb00ff">"$greeting $name!"</font>
<font color="#ff0000">}</font>
</pre>
<br />
<span>You see, I also omitted the curly braces { } around the variables. I only use the curly braces around variables when it makes the code either easier/clearer to read or if it is necessary to use them:</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><font color="#ffffff">declare</font></b><font color="#ff0000"> </font><font color="#ff0000">FOO</font><font color="#F3E651">=</font><font color="#ff0000">bar</font>
<i><font color="#ababab"># Curly braces around FOO are necessary</font></i>
<font color="#ff0000">echo </font><font color="#bb00ff">"foo${FOO}baz"</font>
</pre>
<br />
<span>A few more words on always quoting the variables: For the sake of consistency (and for making ShellCheck happy), I am not against quoting everything I encounter. I also think that the larger the Bash script becomes, the more critical it becomes always to quote variables. That&#39;s because it will be more likely that you might not remember that some of the functions don&#39;t work on values with spaces in them, for example. It&#39;s just that I won&#39;t quote everything in every small script I write. </span><br />
<br />
<h3 style='display: inline' id='prefer-built-in-commands-over-external-commands'>Prefer built-in commands over external commands</h3><br />
<br />
<span>Google recommends using the built-in commands over available external commands where possible:</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="#ababab"># Prefer this:</font></i>
<font color="#ff0000">addition</font><font color="#F3E651">=</font><font color="#ff0000">$(</font><font color="#F3E651">(</font><font color="#ff0000"> X </font><font color="#F3E651">+</font><font color="#ff0000"> Y </font><font color="#F3E651">))</font>
<font color="#ff0000">substitution</font><font color="#F3E651">=</font><font color="#bb00ff">"${string/#foo/bar}"</font>

<i><font color="#ababab"># Instead of this:</font></i>
<font color="#ff0000">addition</font><font color="#F3E651">=</font><font color="#bb00ff">"$(expr "</font><font color="#ff0000">${X}</font><font color="#bb00ff">" + "</font><font color="#ff0000">${Y}</font><font color="#bb00ff">")"</font>
<font color="#ff0000">substitution</font><font color="#F3E651">=</font><font color="#bb00ff">"$(echo "</font><font color="#ff0000">${string}</font><font color="#bb00ff">" | sed -e 's/^foo/bar/')"</font>
</pre>
<br />
<span>I can&#39;t entirely agree here. The external commands (especially sed) are much more sophisticated and powerful than the built-in Bash versions. Sed can do much more than the Bash can ever do by itself when it comes to text manipulation (the name "sed" stands for streaming editor, after all).</span><br />
<br />
<span>I prefer to do light text processing with the Bash built-ins and more complicated text processing with external programs such as sed, grep, awk, cut, and tr. However, there is also medium-light text processing where I would want to use external programs. That is so because I remember using them better than the Bash built-ins. The Bash can get relatively obscure here (even Perl will be more readable then - Side note: I love Perl).</span><br />
<br />
<span>Also, you would like to use an external command for floating-point calculation (e.g., bc) instead of using the Bash built-ins (worth noticing that ZSH supports built-in floating-points).</span><br />
<br />
<span>I even didn&#39;t get started with what you can do with awk (especially GNU Awk), a fully-fledged programming language. Tiny Awk snippets tend to be used quite often in Shell scripts without honouring the real power of Awk. But if you did everything in Perl or Awk or another scripting language, then it wouldn&#39;t be a Bash script anymore, wouldn&#39;t it? ;-)</span><br />
<br />
<h2 style='display: inline' id='my-additions'>My additions</h2><br />
<br />
<h3 style='display: inline' id='use-of--yes--and--no-'>Use of &#39;yes&#39; and &#39;no&#39;</h3><br />
<br />
<span>Bash does not support a boolean type. I tend just to use the strings &#39;yes&#39; and &#39;no&#39; here. I used 0 for false and 1 for true for some time, but I think that the yes/no strings are easier to read. Yes, the Bash script would need to perform string comparisons on every check, but if performance is crucial to you, you wouldn&#39;t want to use a Bash script anyway, correct?</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><font color="#ffffff">declare</font></b><font color="#ff0000"> -r </font><font color="#ff0000">SUGAR_FREE</font><font color="#F3E651">=</font><font color="#ff0000">yes</font>
<b><font color="#ffffff">declare</font></b><font color="#ff0000"> -r </font><font color="#ff0000">I_NEED_THE_BUZZ</font><font color="#F3E651">=</font><font color="#ff0000">no</font>

<font color="#7bc710">buy_soda ()</font><font color="#ff0000"> {</font>
<font color="#ff0000">    </font><b><font color="#ffffff">local</font></b><font color="#ff0000"> -r </font><font color="#ff0000">sugar_free</font><font color="#F3E651">=</font><font color="#ff0000">$1</font>

<font color="#ff0000">    </font><b><font color="#ffffff">if</font></b><font color="#ff0000"> </font><font color="#F3E651">[[</font><font color="#ff0000"> </font><font color="#ff0000">$sugar_free</font><font color="#ff0000"> </font><font color="#F3E651">==</font><font color="#ff0000"> yes </font><font color="#F3E651">]];</font><font color="#ff0000"> </font><b><font color="#ffffff">then</font></b>
<font color="#ff0000">        echo </font><font color="#bb00ff">'Diet Dr. Pepper'</font>
<font color="#ff0000">    </font><b><font color="#ffffff">else</font></b>
<font color="#ff0000">        echo </font><font color="#bb00ff">'Pepsi Coke'</font>
<font color="#ff0000">    </font><b><font color="#ffffff">fi</font></b>
<font color="#ff0000">}</font>

<font color="#ff0000">buy_soda </font><font color="#ff0000">$I_NEED_THE_BUZZ</font>
</pre>
<br />
<h3 style='display: inline' id='non-evil-alternative-to-variable-assignments-via-eval'>Non-evil alternative to variable assignments via eval</h3><br />
<br />
<span>Google is in the opinion that eval should be avoided. I think so too. They list these examples in their guide:</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="#ababab"># What does this set?</font></i>
<i><font color="#ababab"># Did it succeed? In part or whole?</font></i>
<b><font color="#ffffff">eval</font></b><font color="#ff0000"> </font><font color="#ff0000">$(set_my_variables)</font>

<i><font color="#ababab"># What happens if one of the returned values has a space in it?</font></i>
<font color="#ff0000">variable</font><font color="#F3E651">=</font><font color="#bb00ff">"$(eval some_function)"</font>
</pre>
<br />
<span>However, if I want to read variables from another file, I don&#39;t have to use eval here. I only have to source the file:</span><br />
<br />
<pre>
% cat vars.source.sh
declare foo=bar
declare bar=baz
declare bay=foo

% bash -c &#39;source vars.source.sh; echo $foo $bar $baz&#39;
bar baz foo
</pre>
<br />
<span>And suppose I want to assign variables dynamically. In that case, I could just run an external script and source its output (This is how you could do metaprogramming in Bash without the use of eval - write code which produces code for immediate execution):</span><br />
<br />
<pre>
% cat vars.sh
#!/usr/bin/env bash
cat &lt;&lt;END
declare date="$(date)"
declare user=$USER
END

% bash -c &#39;source &lt;(./vars.sh); echo "Hello $user, it is $date"&#39;
Hello paul, it is Sat 15 May 19:21:12 BST 2021
</pre>
<br />
<span>The downside is that ShellCheck won&#39;t be able to follow the dynamic sourcing anymore.</span><br />
<br />
<h3 style='display: inline' id='prefer-pipes-over-arrays-for-list-processing'>Prefer pipes over arrays for list processing</h3><br />
<br />
<span>When I do list processing in Bash, I prefer to use pipes. You can chain them through Bash functions as well, which is pretty neat. Usually, my list processing scripts are of a structure like this:</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><font color="#7bc710">filter_lines ()</font><font color="#ff0000"> {</font>
<font color="#ff0000">    echo </font><font color="#bb00ff">'Start filtering lines in a fancy way!'</font><font color="#ff0000"> </font><font color="#F3E651">&gt;&amp;</font><font color="#bb00ff">2</font>
<font color="#ff0000">    grep </font><font color="#F3E651">...</font><font color="#ff0000"> </font><font color="#F3E651">|</font><font color="#ff0000"> sed </font><font color="#F3E651">....</font>
<font color="#ff0000">}</font>

<font color="#7bc710">process_lines ()</font><font color="#ff0000"> {</font>
<font color="#ff0000">    echo </font><font color="#bb00ff">'Start processing line by line!'</font><font color="#ff0000"> </font><font color="#F3E651">&gt;&amp;</font><font color="#bb00ff">2</font>
<font color="#ff0000">    </font><b><font color="#ffffff">while</font></b><font color="#ff0000"> </font><b><font color="#ffffff">read</font></b><font color="#ff0000"> -r line</font><font color="#F3E651">;</font><font color="#ff0000"> </font><b><font color="#ffffff">do</font></b>
<font color="#ff0000">        </font><font color="#F3E651">...</font><font color="#ff0000"> </font><b><font color="#ffffff">do</font></b><font color="#ff0000"> something and produce a result</font><font color="#F3E651">...</font>
<font color="#ff0000">        echo </font><font color="#bb00ff">"$result"</font>
<font color="#ff0000">    </font><b><font color="#ffffff">done</font></b><font color="#ff0000"> </font>
<font color="#ff0000">}</font>

<i><font color="#ababab"># Do some post-processing of the data</font></i>
<font color="#7bc710">postprocess_lines ()</font><font color="#ff0000"> {</font>
<font color="#ff0000">    echo </font><font color="#bb00ff">'Start removing duplicates!'</font><font color="#ff0000"> </font><font color="#F3E651">&gt;&amp;</font><font color="#bb00ff">2</font>
<font color="#ff0000">    sort -u</font>
<font color="#ff0000">}</font>

<font color="#7bc710">genreate_report ()</font><font color="#ff0000"> {</font>
<font color="#ff0000">    echo </font><font color="#bb00ff">'My boss wants to have a report!'</font><font color="#ff0000"> </font><font color="#F3E651">&gt;&amp;</font><font color="#bb00ff">2</font>
<font color="#ff0000">    tee outfile</font><font color="#F3E651">.</font><font color="#ff0000">txt</font>
<font color="#ff0000">    wc -l outfile</font><font color="#F3E651">.</font><font color="#ff0000">txt</font>
<font color="#ff0000">}</font>

<font color="#7bc710">main ()</font><font color="#ff0000"> {</font>
<font color="#ff0000">    filter_lines </font><font color="#F3E651">|</font>
<font color="#ff0000">        process_lines </font><font color="#F3E651">|</font>
<font color="#ff0000">        postprocess_lines </font><font color="#F3E651">|</font>
<font color="#ff0000">        generate_report</font>
<font color="#ff0000">}</font>

<font color="#ff0000">main</font>
</pre>
<br />
<span>The stdout is always passed as a pipe to the next following stage. The stderr is used for info logging.</span><br />
<br />
<h3 style='display: inline' id='assign-then-shift'>Assign-then-shift</h3><br />
<br />
<span>I often refactor existing Bash code. That leads me to add and removing function arguments quite often. It&#39;s pretty repetitive work changing the $1, $2.... function argument numbers every time you change the order or add/remove possible arguments.</span><br />
<br />
<span>The solution is to use of the "assign-then-shift"-method, which goes like this: "local -r var1=$1; shift; local -r var2=$1; shift". The idea is that you only use "$1" to assign function arguments to named (better readable) local function variables. You will never have to bother about "$2" or above. That is very useful when you constantly refactor your code and remove or add function arguments. It&#39;s something that I picked up from a colleague (a pure Bash wizard) some time ago:</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><font color="#7bc710">some_function ()</font><font color="#ff0000"> {</font>
<font color="#ff0000">    </font><b><font color="#ffffff">local</font></b><font color="#ff0000"> -r </font><font color="#ff0000">param_foo</font><font color="#F3E651">=</font><font color="#bb00ff">"$1"</font><font color="#F3E651">;</font><font color="#ff0000"> </font><b><font color="#ffffff">shift</font></b>
<font color="#ff0000">    </font><b><font color="#ffffff">local</font></b><font color="#ff0000"> -r </font><font color="#ff0000">param_baz</font><font color="#F3E651">=</font><font color="#bb00ff">"$1"</font><font color="#F3E651">;</font><font color="#ff0000"> </font><b><font color="#ffffff">shift</font></b>
<font color="#ff0000">    </font><b><font color="#ffffff">local</font></b><font color="#ff0000"> -r </font><font color="#ff0000">param_bay</font><font color="#F3E651">=</font><font color="#bb00ff">"$1"</font><font color="#F3E651">;</font><font color="#ff0000"> </font><b><font color="#ffffff">shift</font></b>

<font color="#ff0000">    </font><i><font color="#ababab"># ...</font></i>
<font color="#ff0000">}</font>
</pre>
<br />
<span>Want to add a param_baz? Just do this:</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><font color="#7bc710">some_function ()</font><font color="#ff0000"> {</font>
<font color="#ff0000">    </font><b><font color="#ffffff">local</font></b><font color="#ff0000"> -r </font><font color="#ff0000">param_foo</font><font color="#F3E651">=</font><font color="#bb00ff">"$1"</font><font color="#F3E651">;</font><font color="#ff0000"> </font><b><font color="#ffffff">shift</font></b>
<font color="#ff0000">    </font><b><font color="#ffffff">local</font></b><font color="#ff0000"> -r </font><font color="#ff0000">param_bar</font><font color="#F3E651">=</font><font color="#bb00ff">"$1"</font><font color="#F3E651">;</font><font color="#ff0000"> </font><b><font color="#ffffff">shift</font></b>
<font color="#ff0000">    </font><b><font color="#ffffff">local</font></b><font color="#ff0000"> -r </font><font color="#ff0000">param_baz</font><font color="#F3E651">=</font><font color="#bb00ff">"$1"</font><font color="#F3E651">;</font><font color="#ff0000"> </font><b><font color="#ffffff">shift</font></b>
<font color="#ff0000">    </font><b><font color="#ffffff">local</font></b><font color="#ff0000"> -r </font><font color="#ff0000">param_bay</font><font color="#F3E651">=</font><font color="#bb00ff">"$1"</font><font color="#F3E651">;</font><font color="#ff0000"> </font><b><font color="#ffffff">shift</font></b>

<font color="#ff0000">    </font><i><font color="#ababab"># ...</font></i>
<font color="#ff0000">}</font>
</pre>
<br />
<span>Want to remove param_foo? Nothing easier than that:</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><font color="#7bc710">some_function ()</font><font color="#ff0000"> {</font>
<font color="#ff0000">    </font><b><font color="#ffffff">local</font></b><font color="#ff0000"> -r </font><font color="#ff0000">param_bar</font><font color="#F3E651">=</font><font color="#bb00ff">"$1"</font><font color="#F3E651">;</font><font color="#ff0000"> </font><b><font color="#ffffff">shift</font></b>
<font color="#ff0000">    </font><b><font color="#ffffff">local</font></b><font color="#ff0000"> -r </font><font color="#ff0000">param_baz</font><font color="#F3E651">=</font><font color="#bb00ff">"$1"</font><font color="#F3E651">;</font><font color="#ff0000"> </font><b><font color="#ffffff">shift</font></b>
<font color="#ff0000">    </font><b><font color="#ffffff">local</font></b><font color="#ff0000"> -r </font><font color="#ff0000">param_bay</font><font color="#F3E651">=</font><font color="#bb00ff">"$1"</font><font color="#F3E651">;</font><font color="#ff0000"> </font><b><font color="#ffffff">shift</font></b>
<font color="#ff0000">    </font>
<font color="#ff0000">    </font><i><font color="#ababab"># ...</font></i>
<font color="#ff0000">}</font>
</pre>
<br />
<span>As you can see, I didn&#39;t need to change any other assignments within the function. Of course, you would also need to change the function argument lists at every occasion where the function is invoked - you would do that within the same refactoring session.</span><br />
<br />
<h3 style='display: inline' id='paranoid-mode'>Paranoid mode</h3><br />
<br />
<span>I call this the paranoid mode. The Bash will stop executing when a command exits with a status not equal to 0:</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><font color="#ffffff">set</font></b><font color="#ff0000"> -e</font>
<font color="#ff0000">grep -q foo </font><font color="#F3E651">&lt;&lt;&lt;</font><font color="#ff0000"> bar</font>
<font color="#ff0000">echo Jo</font>
</pre>
<br />
<span>Here &#39;Jo&#39; will never be printed out as the grep didn&#39;t find any match. It&#39;s unrealistic for most scripts to run in paranoid mode purely, so there must be a way to add exceptions. Critical Bash scripts of mine tend to look like this:</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="#ababab">#!/usr/bin/env bash</font></i>

<b><font color="#ffffff">set</font></b><font color="#ff0000"> -e</font>

<font color="#7bc710">some_function ()</font><font color="#ff0000"> {</font>
<font color="#ff0000">    </font><i><font color="#ababab"># .. some critical code</font></i>
<font color="#ff0000">    </font><i><font color="#ababab"># ...</font></i>

<font color="#ff0000">    </font><b><font color="#ffffff">set</font></b><font color="#ff0000"> </font><font color="#F3E651">+</font><font color="#ff0000">e</font>
<font color="#ff0000">    </font><i><font color="#ababab"># Grep might fail, but that's OK now</font></i>
<font color="#ff0000">    grep </font><font color="#F3E651">....</font>
<font color="#ff0000">    </font><b><font color="#ffffff">local</font></b><font color="#ff0000"> -i </font><font color="#ff0000">ec</font><font color="#F3E651">=</font><font color="#ff0000">$?</font>
<font color="#ff0000">    </font><b><font color="#ffffff">set</font></b><font color="#ff0000"> -e</font>

<font color="#ff0000">    </font><i><font color="#ababab"># .. critical code continues ...</font></i>
<font color="#ff0000">    </font><b><font color="#ffffff">if</font></b><font color="#ff0000"> </font><font color="#F3E651">[[</font><font color="#ff0000"> </font><font color="#ff0000">$ec</font><font color="#ff0000"> -ne </font><font color="#bb00ff">0</font><font color="#ff0000"> </font><font color="#F3E651">]];</font><font color="#ff0000"> </font><b><font color="#ffffff">then</font></b>
<font color="#ff0000">        </font><font color="#F3E651">:</font><font color="#ff0000"> </font><i><font color="#ababab"># ...</font></i>
<font color="#ff0000">    </font><b><font color="#ffffff">fi</font></b>
<font color="#ff0000">    </font><i><font color="#ababab"># ...</font></i>
<font color="#ff0000">}</font>
</pre>
<br />
<h2 style='display: inline' id='learned'>Learned</h2><br />
<br />
<span>There are also a couple of things I&#39;ve learned from Google&#39;s guide.</span><br />
<br />
<h3 style='display: inline' id='unintended-lexicographical-comparison'>Unintended lexicographical comparison.</h3><br />
<br />
<span>The following looks like a valid Bash code:</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><font color="#ffffff">if</font></b><font color="#ff0000"> </font><font color="#F3E651">[[</font><font color="#ff0000"> </font><font color="#bb00ff">"${my_var}"</font><font color="#ff0000"> </font><font color="#F3E651">&gt;</font><font color="#ff0000"> </font><font color="#bb00ff">3</font><font color="#ff0000"> </font><font color="#F3E651">]];</font><font color="#ff0000"> </font><b><font color="#ffffff">then</font></b>
<font color="#ff0000">    </font><i><font color="#ababab"># True for 4, false for 22.</font></i>
<font color="#ff0000">    do_something</font>
<b><font color="#ffffff">fi</font></b>
</pre>
<br />
<span>... but it is probably an unintended lexicographical comparison. A correct way would be:</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><font color="#ffffff">if</font></b><font color="#ff0000"> </font><font color="#F3E651">((</font><font color="#ff0000"> my_var </font><font color="#F3E651">&gt;</font><font color="#ff0000"> </font><font color="#bb00ff">3</font><font color="#ff0000"> </font><font color="#F3E651">));</font><font color="#ff0000"> </font><b><font color="#ffffff">then</font></b>
<font color="#ff0000">    do_something</font>
<b><font color="#ffffff">fi</font></b>
</pre>
<br />
<span>or</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><font color="#ffffff">if</font></b><font color="#ff0000"> </font><font color="#F3E651">[[</font><font color="#ff0000"> </font><font color="#bb00ff">"${my_var}"</font><font color="#ff0000"> -gt </font><font color="#bb00ff">3</font><font color="#ff0000"> </font><font color="#F3E651">]];</font><font color="#ff0000"> </font><b><font color="#ffffff">then</font></b>
<font color="#ff0000">    do_something</font>
<b><font color="#ffffff">fi</font></b>
</pre>
<br />
<h3 style='display: inline' id='pipestatus'>PIPESTATUS</h3><br />
<br />
<span>I have never used the PIPESTATUS variable before. I knew that it&#39;s there, but I never bothered to understand how it works until now thoroughly.</span><br />
<br />
<span>The PIPESTATUS variable in Bash allows checking of the return code from all parts of a pipe. If it&#39;s only necessary to check the success or failure of the whole pipe, then the following is acceptable:</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><font color="#ff0000">tar -cf - </font><font color="#F3E651">./*</font><font color="#ff0000"> </font><font color="#F3E651">|</font><font color="#ff0000"> </font><font color="#F3E651">(</font><font color="#ff0000"> cd </font><font color="#bb00ff">"${dir}"</font><font color="#ff0000"> </font><font color="#F3E651">&amp;&amp;</font><font color="#ff0000"> tar -xf - </font><font color="#F3E651">)</font>
<b><font color="#ffffff">if</font></b><font color="#ff0000"> </font><font color="#F3E651">((</font><font color="#ff0000"> PIPESTATUS</font><font color="#F3E651">[</font><font color="#bb00ff">0</font><font color="#F3E651">]</font><font color="#ff0000"> </font><font color="#F3E651">!=</font><font color="#ff0000"> </font><font color="#bb00ff">0</font><font color="#ff0000"> </font><font color="#F3E651">||</font><font color="#ff0000"> PIPESTATUS</font><font color="#F3E651">[</font><font color="#bb00ff">1</font><font color="#F3E651">]</font><font color="#ff0000"> </font><font color="#F3E651">!=</font><font color="#ff0000"> </font><font color="#bb00ff">0</font><font color="#ff0000"> </font><font color="#F3E651">));</font><font color="#ff0000"> </font><b><font color="#ffffff">then</font></b>
<font color="#ff0000">    echo </font><font color="#bb00ff">"Unable to tar files to ${dir}"</font><font color="#ff0000"> </font><font color="#F3E651">&gt;&amp;</font><font color="#bb00ff">2</font>
<b><font color="#ffffff">fi</font></b>
</pre>
<br />
<span>However, as PIPESTATUS will be overwritten as soon as you do any other command, if you need to act differently on errors based on where it happened in the pipe, you&#39;ll need to assign PIPESTATUS to another variable immediately after running the command (don&#39;t forget that [ is a command and will wipe out PIPESTATUS).</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><font color="#ff0000">tar -cf - </font><font color="#F3E651">./*</font><font color="#ff0000"> </font><font color="#F3E651">|</font><font color="#ff0000"> </font><font color="#F3E651">(</font><font color="#ff0000"> cd </font><font color="#bb00ff">"${DIR}"</font><font color="#ff0000"> </font><font color="#F3E651">&amp;&amp;</font><font color="#ff0000"> tar -xf - </font><font color="#F3E651">)</font>
<font color="#ff0000">return_codes</font><font color="#F3E651">=(</font><font color="#ff0000"> </font><font color="#bb00ff">"${PIPESTATUS[@]}"</font><font color="#ff0000"> </font><font color="#F3E651">)</font>
<b><font color="#ffffff">if</font></b><font color="#ff0000"> </font><font color="#F3E651">((</font><font color="#ff0000"> return_codes</font><font color="#F3E651">[</font><font color="#bb00ff">0</font><font color="#F3E651">]</font><font color="#ff0000"> </font><font color="#F3E651">!=</font><font color="#ff0000"> </font><font color="#bb00ff">0</font><font color="#ff0000"> </font><font color="#F3E651">));</font><font color="#ff0000"> </font><b><font color="#ffffff">then</font></b>
<font color="#ff0000">    do_something</font>
<b><font color="#ffffff">fi</font></b>
<b><font color="#ffffff">if</font></b><font color="#ff0000"> </font><font color="#F3E651">((</font><font color="#ff0000"> return_codes</font><font color="#F3E651">[</font><font color="#bb00ff">1</font><font color="#F3E651">]</font><font color="#ff0000"> </font><font color="#F3E651">!=</font><font color="#ff0000"> </font><font color="#bb00ff">0</font><font color="#ff0000"> </font><font color="#F3E651">));</font><font color="#ff0000"> </font><b><font color="#ffffff">then</font></b>
<font color="#ff0000">    do_something_else</font>
<b><font color="#ffffff">fi</font></b>
</pre>
<br />
<h2 style='display: inline' id='use-common-sense-and-be-consistent'>Use common sense and BE CONSISTENT.</h2><br />
<br />
<span>The following two paragraphs are thoroughly quoted from the Google guidelines. But they hit the hammer on the head:</span><br />
<br />
<span class='quote'>If you are editing code, take a few minutes to look at the code around you and determine its style. If they use spaces around their if clauses, you should, too. If their comments have little boxes of stars around them, make your comments have little boxes of stars around them too.</span><br />
<br />
<span class='quote'>The point of having style guidelines is to have a common vocabulary of coding so people can concentrate on what you are saying rather than on how you are saying it. We present global style rules here, so people know the vocabulary. But local style is also important. If the code you add to a file looks drastically different from the existing code around it, the discontinuity throws readers out of their rhythm when they go to read it. Try to avoid this.</span><br />
<br />
<br />
<h2 style='display: inline' id='advanced-bash-learning-pro-tip'>Advanced Bash learning pro tip</h2><br />
<br />
<span>I also highly recommend having a read through the "Advanced Bash-Scripting Guide" (not from Google). I use it as the universal Bash reference and learn something new every time I look at it.</span><br />
<br />
<a class='textlink' href='https://tldp.org/LDP/abs/html/'>Advanced Bash-Scripting Guide</a><br />
<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</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 (You are currently reading this)</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>