aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/_notes/bash.md
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2022-04-01 23:15:26 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2022-04-01 23:15:26 +0300
commitb8e6ca2c897ef10e043d8a335f4be12081ffb08c (patch)
tree35572733b0eee4bb25a51e96d4c5fa2f555dfc18 /_notes/bash.md
parentnotes/bash: expand on errexit (diff)
downloadblog-b8e6ca2c897ef10e043d8a335f4be12081ffb08c.tar.gz
blog-b8e6ca2c897ef10e043d8a335f4be12081ffb08c.zip
notes/bash: add syntax highlighting
Diffstat (limited to '')
-rw-r--r--_notes/bash.md135
1 files changed, 79 insertions, 56 deletions
diff --git a/_notes/bash.md b/_notes/bash.md
index 6529b59..1feef07 100644
--- a/_notes/bash.md
+++ b/_notes/bash.md
@@ -10,17 +10,20 @@ subtitle: best practices
`"${#xs[@]}"` doesn't work with `nounset` if `xs` wasn't defined, i.e. was
declared with either of
- local -a xs
- declare -a xs
- local -A xs
- declare -A xs
+```bash
+local -a xs
+declare -a xs
+local -A xs
+declare -A xs
+```
Therefore, if you want to extract the length of an array, append `=()` to the
statements above.
- local -a xs=()
- declare -a xs=()
- ...
+```bash
+local -a xs=()
+declare -a xs=()
+```
And now `"${#xs[@]}"` works with `nounset`.
It doesn't affect expansion (see below) though.
@@ -29,29 +32,37 @@ It doesn't affect expansion (see below) though.
#### Do
- func ${arr[@]+"${arr[@]}"}
+```bash
+func ${arr[@]+"${arr[@]}"}
+```
#### Don't
- func "${arr[@]}" # Doesn't work with `nounset`.
- func "${arr[@]+"${arr[@]}"}" # Doesn't work properly with `declare -a arr=('')`.
+```bash
+func "${arr[@]}" # Doesn't work with `nounset`.
+func "${arr[@]+"${arr[@]}"}" # Doesn't work properly with `declare -a arr=('')`.
+```
### `unset`
#### Do
- unset -v 'arr[x]'
- unset -v 'arr[$i]'
+```bash
+unset -v 'arr[x]'
+unset -v 'arr[$i]'
+```
#### Don't
- unset -v arr[x] # May break due to globbing.
- unset -v arr[$i] # The same as above + a possible problem with quotation.
- unset -v 'arr["x"]' # Doesn't work for some reason.
- unset -v 'arr["]"]' # The same as above; just highlighting the problem with funny characters in array indices.
- unset -v 'arr["$i"]' # Also rejected.
+```bash
+unset -v arr[x] # May break due to globbing.
+unset -v arr[$i] # The same as above + a possible problem with quotation.
+unset -v 'arr["x"]' # Doesn't work for some reason.
+unset -v 'arr["]"]' # The same as above; just highlighting the problem with funny characters in array indices.
+unset -v 'arr["$i"]' # Also rejected.
- # An insightful discussion on the topic: https://lists.gnu.org/archive/html/help-bash/2016-09/msg00020.html.
+# An insightful discussion on the topic: https://lists.gnu.org/archive/html/help-bash/2016-09/msg00020.html.
+```
`errexit`
---------
@@ -63,67 +74,79 @@ useful behaviour.
#### Do
- shopt -s inherit_errexit # Without this, bar will be executed w/ errexit disabled!
+```bash
+shopt -s inherit_errexit # Without this, bar will be executed w/ errexit disabled!
- bar() {
- false
- echo 'should never see this' >&2
- }
+bar() {
+ false
+ echo 'should never see this' >&2
+}
- bar_output="$( bar )"
- foo "$bar_output"
+bar_output="$( bar )"
+foo "$bar_output"
+```
#### Don't
- bar() {
- false
- echo 'should never see this' >&2
- }
+```bash
+bar() {
+ false
+ echo 'should never see this' >&2
+}
- foo "$( bar )" # Even with errexit, foo will still get executed.
- # More than that, the script will print 'should never see this'!
+foo "$( bar )" # Even with errexit, foo will still get executed.
+ # More than that, the script will print 'should never see this'!
+```
### Process substitution
#### Do
- output="$( command )"
+```bash
+output="$( command )"
- while IFS= read -r line; do
- process_line "$line"
- done <<< "$output"
+while IFS= read -r line; do
+ process_line "$line"
+done <<< "$output"
+```
#### Don't
- # This causes some bash insanity where you cannot change directories or set
- # variables inside a loop: http://mywiki.wooledge.org/BashFAQ/024
- command | while IFS= read -r line; do
- process_line "$line"
- done
+```bash
+# This causes some bash insanity where you cannot change directories or set
+# variables inside a loop: http://mywiki.wooledge.org/BashFAQ/024
+command | while IFS= read -r line; do
+ process_line "$line"
+done
- # errexit doesn't work here no matter what:
- while IFS= read -r line; do
- process_line "$line"
- done < <( command )
- echo 'should never see this'
+# errexit doesn't work here no matter what:
+while IFS= read -r line; do
+ process_line "$line"
+done < <( command )
+echo 'should never see this'
+```
### Functions
#### Do
- foo() {
- false
- echo 'should never see this' >&2
- }
+```bash
+foo() {
+ false
+ echo 'should never see this' >&2
+}
- foo
- echo ok
+foo
+echo ok
+```
#### Don't
- if foo; then
- echo ok # foo will still print 'should never see this'.
- fi
+```bash
+if foo; then
+ echo ok # foo will still print 'should never see this'.
+fi
- foo && echo ok # Same here.
- foo || echo ok
+foo && echo ok # Same here.
+foo || echo ok
+```