aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/_notes
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2019-09-30 05:26:59 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2019-09-30 05:26:59 +0300
commita0fa71c92c4f6f42f3f0b212158c22dedf1d52b9 (patch)
tree1fa2f0e6a50a37218864ddfe067f1ec5b4b244dd /_notes
parentimport markdown.md from egor-tensin/notes.git (diff)
downloadblog-a0fa71c92c4f6f42f3f0b212158c22dedf1d52b9.tar.gz
blog-a0fa71c92c4f6f42f3f0b212158c22dedf1d52b9.zip
import bash.md from egor-tensin/notes.git
Diffstat (limited to '_notes')
-rw-r--r--_notes/bash.md67
1 files changed, 67 insertions, 0 deletions
diff --git a/_notes/bash.md b/_notes/bash.md
new file mode 100644
index 0000000..c78333e
--- /dev/null
+++ b/_notes/bash.md
@@ -0,0 +1,67 @@
+---
+layout: note
+title: GNU bash
+---
+(Associative) arrays
+--------------------
+
+### Declaration
+
+`"${#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
+
+Therefore, if you want to extract the length of an array, append `=()` to the
+statements above.
+
+ local -a xs=()
+ declare -a xs=()
+ ...
+
+And now `"${#xs[@]}"` works with `nounset`.
+It doesn't affect expansion (see below) though.
+
+### Expansion
+
+#### Do
+
+ func ${arr[@]+"${arr[@]}"}
+
+#### Don't
+
+ 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]'
+
+#### 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.
+
+ # An insightful discussion on the topic: https://lists.gnu.org/archive/html/help-bash/2016-09/msg00020.html.
+
+`errexit`
+---------
+
+### Do
+
+ bar_output="$( bar )"
+ foo "$bar_output"
+
+### Don't
+
+ foo "$( bar )" # With `errexit`, foo will still get executed.
+ # I don't know why.