From 98d438b497e1f2da1776367825552910b1a52bcc Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Tue, 22 Nov 2016 03:08:59 +0300 Subject: bash.md: how to declare arrays --- bash.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/bash.md b/bash.md index 628bfa8..6f729a0 100644 --- a/bash.md +++ b/bash.md @@ -4,6 +4,26 @@ 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 -- cgit v1.2.3