bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 'mapfile -O1 array' breaks '[[ -v array ]]'


From: Chet Ramey
Subject: Re: 'mapfile -O1 array' breaks '[[ -v array ]]'
Date: Mon, 17 Apr 2023 14:53:58 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.10.0

On 4/17/23 1:23 PM, Wiley Young wrote:

Bash Version: 5.1
Patch Level: 16
Release Status: release

Description:
         When an array 'a_foo' is created with 'mapfile -d '' -t', it is
tested with '[[ -v a_foo ]]' and '[[' returns 0. When '-O 1' is added to
mapfile, '[[ -v a_foo ]]' returns non-0.

This is because when you use an array variable name without a subscript,
it's treated as equivalent to using a subscript of 0.

If you want to check whether array a has any set elements, you can use
[[ -v a[@] ]] or [[ ${#a[@]} > 0 ]].

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]