summaryrefslogtreecommitdiff
path: root/gemfeed/2021-05-16-personal-bash-coding-style-guide.html
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2021-05-31 10:09:19 +0100
committerPaul Buetow <paul@buetow.org>2021-05-31 10:09:19 +0100
commitd3a70f706d57530e6c3a12364af0fdcf51ec6e20 (patch)
tree2eb8d872ee3ae5254850c4cc9e2f3372659f594e /gemfeed/2021-05-16-personal-bash-coding-style-guide.html
parentc7d03dc1b79d2214db40e322a31d1844b1c64d87 (diff)
Publishing new version
Diffstat (limited to 'gemfeed/2021-05-16-personal-bash-coding-style-guide.html')
-rw-r--r--gemfeed/2021-05-16-personal-bash-coding-style-guide.html16
1 files changed, 8 insertions, 8 deletions
diff --git a/gemfeed/2021-05-16-personal-bash-coding-style-guide.html b/gemfeed/2021-05-16-personal-bash-coding-style-guide.html
index 59a2d5c4..2bb03c9a 100644
--- a/gemfeed/2021-05-16-personal-bash-coding-style-guide.html
+++ b/gemfeed/2021-05-16-personal-bash-coding-style-guide.html
@@ -67,11 +67,11 @@ h2, h3 {
<h2>My modifications</h2>
<p>These are my modifications to the Google Guide.</p>
<h3>Shebang</h3>
-<p>Google recommends using always</p>
+<p>Google recommends using always...</p>
<pre>
#!/bin/bash
</pre>
-<p>as the shebang line, but that does not work on all Unix and Unix-like operating systems (e.g., the *BSDs don't have Bash installed to /bin/bash). Better is:</p>
+<p>... as the shebang line, but that does not work on all Unix and Unix-like operating systems (e.g., the *BSDs don't have Bash installed to /bin/bash). Better is:</p>
<pre>
#!/usr/bin/env bash
</pre>
@@ -101,7 +101,7 @@ command1 |
command4
</pre>
<h3>Quoting your variables</h3>
-<p>Google recommends always quote your variables. Generally, you should do 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:</p>
+<p>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:</p>
<pre>
greet () {
local -r greeting="${1}"
@@ -135,10 +135,10 @@ substitution="${string/#foo/bar}"
addition="$(expr "${X}" + "${Y}")"
substitution="$(echo "${string}" | sed -e 's/^foo/bar/')"
</pre>
-<p>I can't entirely agree here. The external commands (especially sed) are much more sophisticated and powerful than the Bash built-in 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).</p>
-<p>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 the case of 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).</p>
+<p>I can'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).</p>
+<p>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).</p>
<p>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).</p>
-<p>I even didn'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 honoring the real power of Awk. But if you did everything in Perl or Awk or another scripting language, then it wouldn't be a Bash script anymore, wouldn't it? ;-)</p>
+<p>I even didn'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't be a Bash script anymore, wouldn't it? ;-)</p>
<h2>My additions</h2>
<h3>Use of 'yes' and 'no'</h3>
<p>Bash does not support a boolean type. I tend just to use the strings 'yes' and 'no' 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't want to use a Bash script anyway, correct?</p>
@@ -169,7 +169,7 @@ eval $(set_my_variables)
variable="$(eval some_function)"
</pre>
-<p>However, if I want to read variables from another file, I don't have to use eval here. I just source the file:</p>
+<p>However, if I want to read variables from another file, I don't have to use eval here. I only have to source the file:</p>
<pre>
% cat vars.source.sh
declare foo=bar
@@ -232,7 +232,7 @@ main
<p>The stdout is always passed as a pipe to the next following stage. The stderr is used for info logging.</p>
<h3>Assign-then-shift</h3>
<p>I often refactor existing Bash code. That leads me to add and removing function arguments quite often. It's pretty repetitive work changing the $1, $2.... function argument numbers every time you change the order or add/remove possible arguments.</p>
-<p>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's something what I picked up from a colleague (a pure Bash wizard) some time ago:</p>
+<p>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's something that I picked up from a colleague (a pure Bash wizard) some time ago:</p>
<pre>
some_function () {
local -r param_foo="$1"; shift