Path: csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail From: Martin Schulte Newsgroups: gnu.bash.bug Subject: Re: Two states of empty arrays Date: Thu, 12 Dec 2019 22:34:52 +0100 Lines: 37 Approved: bug-bash@gnu.org Message-ID: References: <79b6365d-4c09-7122-25c3-4564fd304948@case.edu> <20191212223452.157114bb8c51cb2689b793b8@schrader-schulte.de> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: usenet.stanford.edu 1576186508 3351 209.51.188.17 (12 Dec 2019 21:35:08 GMT) X-Complaints-To: action@cs.stanford.edu Cc: gnu-bash-bug@moderators.isc.org To: =?ISO-8859-1?Q?L=E9a?= Gris Envelope-to: bug-bash@gnu.org Delivered-To: gnu-bash-bug@moderators.isc.org In-Reply-To: X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.31; x86_64-pc-linux-gnu) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:19f0:5c01:c22:5400:1ff:fead:27ae X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <20191212223452.157114bb8c51cb2689b793b8@schrader-schulte.de> X-Mailman-Original-References: <79b6365d-4c09-7122-25c3-4564fd304948@case.edu> Xref: csiph.com gnu.bash.bug:15723 Hello L=E9a! L=E9a Gris wrote: > I was trying to play the the -v test to detect when an array or > associative array has been declared, not necessarily assigned entries > key, values, to not error when Bash runs with -o nounset Just for the curious: What is your attention here? I think that most useful questions (Is there an element in the array? Is there a value for a given key?) can be answered in a simpler way: #!/bin/bash set -o nounset # From what I learned today it seems to be good practice to always # assign and empty array to when declaring an associative array: declare -A assoc=3D() echo ${#assoc[@]} # Are there elements in it? assoc[key1]=3Dval1 assoc[key2]=3D for key in key1 key2 key3; do if [[ -n ${assoc[$key]+isset} ]]; then echo "Element for $key is set" else echo "No Element for $key" fi done Best regards, Martin