# What is the definition of an array being set?

1. The definition for an array being set is from bash manual:

An array variable is considered set if a subscript has been assigned a value. The null string is a valid value.

• Does it mean that an array is set if and only if the number of its elements is greater than zero?

• Is the following array set, `myarr=()`?

• The manual also says

A parameter is set if it has been assigned a value. The null string is a valid value.

Since an array is also a parameter, how does the definition of an array being set agree with the definition of a parameter being set?

For example, `myvar=""` is set, but is `myarr=()` set? Has `myarr` been assigned a value, which is null?

2. There is a different definition for an array being set from https://unix.stackexchange.com/a/246703/674

For shells that support arrays, except for yash and zsh that would not report as set array variables unless the element of indice 0 has been set.

For bash (but not ksh93 nor zsh), for variables of type associative array, that would not report them as set unless their element of key "0" has been set.

Is this definition incorrect, because an array name without subscription actually refers to the element indexed by index/key `0` instead of referring to the entire array, according to Bash manual

Referencing an array variable without a subscript is equivalent to referencing with a subscript of 0.

So is this definition actually for the element indexed by index/key `0` being set, not for the array being set?

For example, `\${myarr+1}` and `[[ -v myarr ]]` are the applications to an array `myarr` the usual ways of testing if a variable/parameter is set. Which does `myarr` in them mean: `myarr`, or the entire array `myarr`?

3. What is POSIX definition for a shell array being set?

• You can try yourself: `set -u; x=(=b); echo \$x` or `set -u; declare -A x; x=([a]=b); echo \$x`, then try the same with 0 instead of 1 and `a` respectively. – choroba May 3 '16 at 20:05
• @choroba: Thanks. Does `x` in `echo \$x` mean `x` or the array? Does your example test which one is set, `x` or the array? – Tim May 3 '16 at 21:56
• Why do you need to know whether an array is set? – choroba May 3 '16 at 22:15
• You answered your own question in the first line: "What is the definition of an array being set?" "The definition for an array being set is from bash manual: An array variable is considered set if a subscript has been assigned a value. The null string is a valid value." At the very least you need to fix the question title, or this will be closed as unclear what you're asking (my prediction). – Wildcard May 3 '16 at 23:43
• @Tim: If we knew why you needed it, we could recommend an alternative solution. – choroba May 4 '16 at 7:26

• Does it mean that an array is set if and only if the number of its elements is greater than zero?

Yes.

• Is the following array set, myarr=()?

No.

• how does the definition of an array being set agree with the definition of a parameter being set?

Both are meant to be equal, I see no problem here:

• A parameter is set if it has been assigned a value. The null string is a valid value.

• An array variable is considered set if ... has been assigned a value. The null string is a valid value.

1. There is a different definition ...

What the answer at https://unix.stackexchange.com/a/246703/674 is describing is the details of how the test `[ -n "\${var+set}" ]` work, not defining what is set or not, not for normal variables, not for arrays.

Please note the `that` in `that would not report ...`.

1. What is POSIX definition for a shell array being set?

There are no "shell arrays" in POSIX.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8680636,"math_prob":0.8410108,"size":2869,"snap":"2020-34-2020-40","text_gpt3_token_len":713,"char_repetition_ratio":0.14659686,"word_repetition_ratio":0.25581396,"special_character_ratio":0.25235274,"punctuation_ratio":0.12602292,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9689264,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-25T06:59:20Z\",\"WARC-Record-ID\":\"<urn:uuid:ad662161-4787-4f23-9edf-f190961aac1c>\",\"Content-Length\":\"159218\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3ecb3469-b87f-4f9a-b23b-c72d0e8738d2>\",\"WARC-Concurrent-To\":\"<urn:uuid:41639b69-661e-4784-baaa-9be57f731675>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://unix.stackexchange.com/questions/280893/what-is-the-definition-of-an-array-being-set\",\"WARC-Payload-Digest\":\"sha1:5QH6G3CCEI4LAXHG775OSVQNPYI5LKOW\",\"WARC-Block-Digest\":\"sha1:ONYSLXDZCGLKASEFAG277WM7QTQQ67S7\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600400222515.48_warc_CC-MAIN-20200925053037-20200925083037-00624.warc.gz\"}"}