Re: ${!variable@operator} does not work for variables without values; inconsistencies between present and absent [@] for @A and @a
On 2/20/20 7:34 PM, Arfrever Frehtes Taifersar Arahesis wrote: >> Same answer as previously: maybe it should display the attributes >> even thought the variable is unset. > > Yes, I think that @A, [@]@A, @a and [@]@a should work for unset variables. OK, we'll try that and see how it works. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/
Re: ${!variable@operator} does not work for variables without values; inconsistencies between present and absent [@] for @A and @a
On 2/20/20 6:54 PM, Arfrever Frehtes Taifersar Arahesis wrote: > Chet Ramey 2020-02-20 21:22 UTC: >> On 2/19/20 7:46 PM, Arfrever Frehtes Taifersar Arahesis wrote: >>> But I am not interested in any ${!varname[@]}, but instead in applying >>> @operator transformations. >> >> OK, let's see how these work. >> >> Given the following >> >> VAR2=(aaa bbb) >> varname=VAR2 >> >> What does >> >> echo ${!varname[@]@Q} >> >> output? >> >> You first have to expand `varname[@]' as an indirect reference. Since >> varname is a scalar variable, varname[@] expands to the same thing as >> varname, which is VAR2. Now you expand VAR2, which, since VAR2 is an >> array variable, is the same as VAR2[0]. That gives you "aaa", so the >> output is 'aaa'. > > Your explanation is convincing for varname=VAR2 but now I would expect > different results for varname=VAR2[@]. > > Current actual results: > $ VAR2=(aaa bbb) > $ varname="VAR2[@]" > $ echo "${VAR2@Q}" > 'aaa' > $ echo "${VAR2[@]@Q}" > 'aaa' 'bbb' > $ echo "${!varname@Q}" > > $ echo "${!varname[@]@Q}" > > $ > > Expected results for last 2 commands: > > $ echo "${!varname@Q}" > 'aaa' 'bbb' This is correct output, and there's a bug here. > $ echo "${!varname[@]@Q}" > bash: ${VAR2[@][@]@Q}: bad substitution# This is example from > direct usage of [@][@] This is not; it should echo the same as the previous case, for the same reason as I stated above (expand varname[@]; it expands to the same thing as varname since varname is not an array variable; use the expansion to VAR2[@]). -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/
Re: ${!variable@operator} does not work for variables without values; inconsistencies between present and absent [@] for @A and @a
On 2/20/20 6:23 PM, Arfrever Frehtes Taifersar Arahesis wrote: > Even more strangely, quoting apparently matters... Yes, this is a bug: > $ echo "${VAR1[@]@A}" > declare -rl VAR1='' >> The question is whether the unset variables should display commands to >> set the attributes (@A) or display any attributes (@a). > > I think that it would be expected that @A prints the same as 'declare > -p' for given variable. That's not unreasonable, but it takes some consideration. You have to be careful separating out these cases from the usual case, where a variable name in a brace expansion is expanded to nothing if it's unset. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/
Re: ${!variable@operator} does not work for variables without values; inconsistencies between present and absent [@] for @A and @a
2020-02-20 21:36 GMT+01:00, Chet Ramey : > On 2/19/20 4:03 PM, Arfrever Frehtes Taifersar Arahesis wrote: >> ${!variable@operator} does not work for variables without values. >> See empty values for all occurrences of ${!var@...} below. >> >> ${variable@A} does not work for scalar variables without values, but >> interestingly ${variable[@]@A} works for them. >> See difference between ${VAR1@A} and ${VAR1[@]@A} below. >> However neither ${variable@A} nor ${variable[@]@A} works for arrays >> without values. >> >> Both ${variable@a} and ${variable[@]@a} work for scalar variables >> without values. >> However ${variable[@]@a} does not work for arrays without values. > > Same answer as previously: maybe it should display the attributes > even thought the variable is unset. Yes, I think that @A, [@]@A, @a and [@]@a should work for unset variables. As mentioned previously, @a and [@]@a already works for unset scalar variables, and @a works for unset arrays, but [@]@a does not work for unset arrays (while it works for set arrays). Examples for @a and [@]@a: $ declare -lr VAR1 $ declare -lr VAR2=zzz $ declare -alr VAR3 $ declare -alr VAR4=(zzz zzz) $ declare -Alr VAR5 $ declare -Alr VAR6=([0]=zzz [1]=zzz) $ declare -p VAR{1,2,3,4,5,6} declare -rl VAR1 declare -rl VAR2="zzz" declare -arl VAR3 declare -arl VAR4=([0]="zzz" [1]="zzz") declare -Arl VAR5 declare -Arl VAR6=([1]="zzz" [0]="zzz" ) $ echo "${VAR1@a}" rl $ echo "${VAR1[@]@a}" rl $ echo "${VAR2@a}" rl $ echo "${VAR2[@]@a}" rl $ echo "${VAR3@a}" arl $ echo "${VAR3[@]@a}" $ echo "${VAR4@a}" arl $ echo "${VAR4[@]@a}" arl arl $ echo "${VAR5@a}" Arl $ echo "${VAR5[@]@a}" $ echo "${VAR6@a}" Arl $ echo "${VAR6[@]@a}" Arl Arl $ -- Arfrever Frehtes Taifersar Arahesis
Re: ${!variable@operator} does not work for variables without values; inconsistencies between present and absent [@] for @A and @a
Chet Ramey 2020-02-20 21:22 UTC: > On 2/19/20 7:46 PM, Arfrever Frehtes Taifersar Arahesis wrote: >> But I am not interested in any ${!varname[@]}, but instead in applying >> @operator transformations. > > OK, let's see how these work. > > Given the following > > VAR2=(aaa bbb) > varname=VAR2 > > What does > > echo ${!varname[@]@Q} > > output? > > You first have to expand `varname[@]' as an indirect reference. Since > varname is a scalar variable, varname[@] expands to the same thing as > varname, which is VAR2. Now you expand VAR2, which, since VAR2 is an > array variable, is the same as VAR2[0]. That gives you "aaa", so the > output is 'aaa'. Your explanation is convincing for varname=VAR2 but now I would expect different results for varname=VAR2[@]. Current actual results: $ VAR2=(aaa bbb) $ varname="VAR2[@]" $ echo "${VAR2@Q}" 'aaa' $ echo "${VAR2[@]@Q}" 'aaa' 'bbb' $ echo "${!varname@Q}" $ echo "${!varname[@]@Q}" $ Expected results for last 2 commands: $ echo "${!varname@Q}" 'aaa' 'bbb' $ echo "${!varname[@]@Q}" bash: ${VAR2[@][@]@Q}: bad substitution# This is example from direct usage of [@][@] $ -- Arfrever Frehtes Taifersar Arahesis
Re: ${!variable@operator} does not work for variables without values; inconsistencies between present and absent [@] for @A and @a
Chet Ramey 2020-02-20 20:41 UTC: > On 2/19/20 5:22 PM, Arfrever Frehtes Taifersar Arahesis wrote: >>> ${variable@A} does not work for scalar variables without values, but >>> interestingly ${variable[@]@A} works for them. >> >> More precisely, ${variable[@]@A} is non-empty, but not exactly correct. >> >>> See difference between ${VAR1@A} and ${VAR1[@]@A} below. >> >> ${VAR1[@]@A} is: >> declare -rl VAR1=''" >> But should be: >> declare -rl VAR1 >> As in output of 'declare -p VAR1'. > > The output is two blank lines: > > $ cat x6b > declare -lr VAR1 > > echo ${VAR1@A} > echo ${VAR1[@]@A} > $ ../bash-5.0-patched/bash ./x6b > > Even more strangely, quoting apparently matters... $ declare -lr VAR1 $ echo ${VAR1@A} $ echo ${VAR1[@]@A} $ echo "${VAR1@A}" $ echo "${VAR1[@]@A}" declare -rl VAR1='' $ declare -p VAR1 declare -rl VAR1 $ > The question is whether the unset variables should display commands to > set the attributes (@A) or display any attributes (@a). I think that it would be expected that @A prints the same as 'declare -p' for given variable. -- Arfrever Frehtes Taifersar Arahesis
Re: ${!variable@operator} does not work for variables without values; inconsistencies between present and absent [@] for @A and @a
On 2/19/20 7:46 PM, Arfrever Frehtes Taifersar Arahesis wrote: > But I am not interested in any ${!varname[@]}, but instead in applying > @operator transformations. OK, let's see how these work. Given the following VAR2=(aaa bbb) varname=VAR2 What does echo ${!varname[@]@Q} output? You first have to expand `varname[@]' as an indirect reference. Since varname is a scalar variable, varname[@] expands to the same thing as varname, which is VAR2. Now you expand VAR2, which, since VAR2 is an array variable, is the same as VAR2[0]. That gives you "aaa", so the output is 'aaa'. Now, let's look at echo ${VAR2[@]@Q} which (obviously) outputs 'aaa' 'bbb'. I don't think I need to go through how that happens. Finally, take a look at echo ${!VAR2[@]@Q} We go through the same process as before to find the value of VAR2[@], which turns out to be "aaa bbb" (we flatten it to a string for this expansion, since we want a variable name). That happens to be an invalid variable name, since it contains a space, so we get x6d: line 8: aaa bbb: invalid variable name Since Eli brought up ${!varname[@]}, I'll mention that special case. That's an expansion, not a parameter. It's a seemingly minor distinction, but the expansion is the entire contents of the word between the braces, without an operator. That's why the above cases work the way they do, and echo ${!VAR2[@]} outputs `0 1'. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/
Re: ${!variable@operator} does not work for variables without values; inconsistencies between present and absent [@] for @A and @a
On 2/19/20 5:22 PM, Arfrever Frehtes Taifersar Arahesis wrote: >> ${variable@A} does not work for scalar variables without values, but >> interestingly ${variable[@]@A} works for them. > > More precisely, ${variable[@]@A} is non-empty, but not exactly correct. > >> See difference between ${VAR1@A} and ${VAR1[@]@A} below. > > ${VAR1[@]@A} is: > declare -rl VAR1=''" > But should be: > declare -rl VAR1 > As in output of 'declare -p VAR1'. The output is two blank lines: $ cat x6b declare -lr VAR1 echo ${VAR1@A} echo ${VAR1[@]@A} $ ../bash-5.0-patched/bash ./x6b The question is whether the unset variables should display commands to set the attributes (@A) or display any attributes (@a). -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/
Re: ${!variable@operator} does not work for variables without values; inconsistencies between present and absent [@] for @A and @a
On 2/19/20 4:03 PM, Arfrever Frehtes Taifersar Arahesis wrote: > ${!variable@operator} does not work for variables without values. > See empty values for all occurrences of ${!var@...} below. > > ${variable@A} does not work for scalar variables without values, but > interestingly ${variable[@]@A} works for them. > See difference between ${VAR1@A} and ${VAR1[@]@A} below. > However neither ${variable@A} nor ${variable[@]@A} works for arrays > without values. > > Both ${variable@a} and ${variable[@]@a} work for scalar variables > without values. > However ${variable[@]@a} does not work for arrays without values. Same answer as previously: maybe it should display the attributes even thought the variable is unset. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/
Re: ${!variable@operator} does not work for variables without values; inconsistencies between present and absent [@] for @A and @a
On 2/19/20 7:46 PM, Arfrever Frehtes Taifersar Arahesis wrote: > Eli Schwartz 2020-02-20 23:49 UTC: >> Your examples are all (still) broken. > > This would affect only 10 examples from 120, so only 8.33 % of > examples, far from all examples. [...] > Majority (3 of 4) of bugs reported by me in this thread are unaffected > by above discussion about ${!...} and are certainly still valid. But your examples are founded in a misunderstanding of how bash variables work, why should I expend the mental bandwidth to unroll your micro-optimized for loop full of eval to figure out which examples are valid? You've immediately jumped from "I don't get the result I expected" to "there's a bug in bash". I've pointed out that there are gaps in your understanding of how bash works. Maybe it's time to revisit your initial assumptions going in, open yourself up to the possibility that you might be wrong, rather than bash, and finally, please, *please* just elaborate your test cases in a readable manner without making them conditional on correct use of eval. Then we could all discuss whether a specific bit of behavior makes sense. -- Eli Schwartz Arch Linux Bug Wrangler and Trusted User signature.asc Description: OpenPGP digital signature
Re: ${!variable@operator} does not work for variables without values; inconsistencies between present and absent [@] for @A and @a
> On 2/19/20 4:03 PM, Arfrever Frehtes Taifersar Arahesis wrote: > > > However neither ${variable@A} nor ${variable[@]@A} works for arrays > > without values. > > So you mean for unset variables, correct? Since the array variables > haven't been assigned a value, they are unset, like VAR3 in your > script. I suppose the question is whether or not to display the command that would restore the variable with its attributes and not its value. That's a legit question. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/
Re: ${!variable@operator} does not work for variables without values; inconsistencies between present and absent [@] for @A and @a
On 2/19/20 4:03 PM, Arfrever Frehtes Taifersar Arahesis wrote: > However neither ${variable@A} nor ${variable[@]@A} works for arrays > without values. So you mean for unset variables, correct? Since the array variables haven't been assigned a value, they are unset, like VAR3 in your script. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/
Re: ${!variable@operator} does not work for variables without values; inconsistencies between present and absent [@] for @A and @a
On 2/19/20 4:03 PM, Arfrever Frehtes Taifersar Arahesis wrote: > ${variable@A} does not work for scalar variables without values, but > interestingly ${variable[@]@A} works for them. What does this mean? If you have, for instance, declare -lr VAR1 echo ${VAR1@A} echo ${VAR1[@]@A} you get two empty lines. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/
Re: ${!variable@operator} does not work for variables without values; inconsistencies between present and absent [@] for @A and @a
On 2/19/20 4:03 PM, Arfrever Frehtes Taifersar Arahesis wrote: > ${!variable@operator} does not work for variables without values. > See empty values for all occurrences of ${!var@...} below. I think you already realized that your test for this case was flawed. For instance, given var=VAR2 operator=Q eval "echo \"'\${!${var}@${operator}}'\"" you end up expanding ${!VAR2@Q}. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/
Re: ${!variable@operator} does not work for variables without values; inconsistencies between present and absent [@] for @A and @a
On Thu, Feb 20, 2020 at 01:46:25AM +0100, Arfrever Frehtes Taifersar Arahesis wrote: > Is there any way for using ${!variable} combined with > ${variable@operator} No.
Re: ${!variable@operator} does not work for variables without values; inconsistencies between present and absent [@] for @A and @a
Eli Schwartz 2020-02-20 23:49 UTC: > Your examples are all (still) broken. This would affect only 10 examples from 120, so only 8.33 % of examples, far from all examples. > You cannot use ${!ref[@]}, you need the array subscript as part of the > set value of "ref" and then indirectly refer to ref. > > $ declare -A VAR2=([foo]=bar [baz]="how interesting") > $ args "${!VAR2[@]}" > > ... > And, as predicted, if instead of using ${!ref[@]} you use > ${!varname[@]}, you get meaningful information. But I am not interested in any ${!varname[@]}, but instead in applying @operator transformations. >From `man bash`: ``` ${parameter@operator} Parameter transformation. The expansion is either a transformation of the value of parameter or information about parameter itself, de‐ pending on the value of operator. Each operator is a single letter: Q The expansion is a string that is the value of parameter quoted in a format that can be reused as input. E The expansion is a string that is the value of parameter with backslash escape sequences expanded as with the $'...' quoting mecha‐ nism. P The expansion is a string that is the result of expanding the value of parameter as if it were a prompt string (see PROMPTING be‐ low). A The expansion is a string in the form of an assignment statement or declare command that, if evaluated, will recreate parameter with its attributes and value. a The expansion is a string consisting of flag values representing parameter's attributes. If parameter is @ or *, the operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parame‐ ter is an array variable subscripted with @ or *, the operation is applied to each member of the array in turn, and the expansion is the resultant list. ``` Is there any way for using ${!variable} combined with ${variable@operator} to get useful results for multi-elemental arrays? If not, then I can still use `eval` :) . $ ARRAY=(あ い う え お) $ REF=ARRAY $ eval "echo \"\${${REF}[@]@A}\"" declare -a ARRAY=([0]="あ" [1]="い" [2]="う" [3]="え" [4]="お") $ eval "echo \"\${${REF}[@]@a}\"" a a a a a $ echo "${ARRAY[@]@A}" declare -a ARRAY=([0]="あ" [1]="い" [2]="う" [3]="え" [4]="お") $ echo "${ARRAY[@]@a}" a a a a a $ Majority (3 of 4) of bugs reported by me in this thread are unaffected by above discussion about ${!...} and are certainly still valid. -- Arfrever Frehtes Taifersar Arahesis
Re: ${!variable@operator} does not work for variables without values; inconsistencies between present and absent [@] for @A and @a
On 2/19/20 6:18 PM, Arfrever Frehtes Taifersar Arahesis wrote: > While described problems exist, there were typos in original reproduction > steps. > > Corrected output also reveals another bug: > ${!variable[@]@A} for (indexed or associative) arrays does not include > indexes. Your examples are all (still) broken. Do not try to reproduce bugs using: VAR1=(aaa) Instead, use: VAR1=(aaa bbb) Reformulate all test cases where you expect the bbb to be printed, but it isn't. $ VAR1=(aaa bbb) $ ref=VAR1 $ echo "${!ref[@]}" 0 $ echo "${!ref[@]@A}" declare -a VAR2='aaa' $ ref2=VAR1[@] $ args() { printf '<%s> ' "$@"; printf \\n; } $ args "${!ref2}" $ args "${!ref2@A}" <> You cannot use ${!ref[@]}, you need the array subscript as part of the set value of "ref" and then indirectly refer to ref. $ declare -A VAR2=([foo]=bar [baz]="how interesting") $ args "${!VAR2[@]}" From man bash: ``` ${!name[@]} ${!name[*]} List of array keys. If name is an array variable, expands to the list of array indices (keys) assigned in name. If name is not an array, expands to 0 if name is set and null otherwise. When @ is used and the expansion appears within double quotes, each key expands to a separate word. ``` And, as predicted, if instead of using ${!ref[@]} you use ${!varname[@]}, you get meaningful information. If you try to look at the array keys of "ref" itself, all it prints is the number 0, because: ref=VAR1 and since it is set and non-null... -- Eli Schwartz Arch Linux Bug Wrangler and Trusted User signature.asc Description: OpenPGP digital signature
Re: ${!variable@operator} does not work for variables without values; inconsistencies between present and absent [@] for @A and @a
While described problems exist, there were typos in original reproduction steps. Corrected output also reveals another bug: ${!variable[@]@A} for (indexed or associative) arrays does not include indexes. Example part of output: var=VAR4 ${!var@A}:'declare -arl VAR4='zzz'' ${!var[@]@A}: 'declare -arl VAR4='zzz'' ${VAR4@A}:'declare -arl VAR4='zzz'' ${VAR4[@]@A}: 'declare -arl VAR4=([0]="zzz")' ${!var[@]@A} would be expected to be the same as ${VAR4[@]@A}. Corrected full reproduction steps with better formatting of 'for' loops: $ declare -lr VAR1 $ declare -lr VAR2=zzz $ declare -alr VAR3 $ declare -alr VAR4=(zzz) $ declare -Alr VAR5 $ declare -Alr VAR6=([0]=zzz) $ declare -p VAR{1,2,3,4,5,6} declare -rl VAR1 declare -rl VAR2="zzz" declare -arl VAR3 declare -arl VAR4=([0]="zzz") declare -Arl VAR5 declare -Arl VAR6=([0]="zzz" ) $ for operator in Q E P A a ; do > for var in VAR{1,2,3,4,5,6} ; do > echo "var=${var}" > eval "echo \"\\\${!var@${operator}}:'\${!var@${operator}}'\"" > eval "echo \"\\\${!var[@]@${operator}}: '\${!var[@]@${operator}}'\"" > eval "echo \"\\\${${var}@${operator}}:'\${${var}@${operator}}'\"" > eval "echo \"\\\${${var}[@]@${operator}}: '\${${var}[@]@${operator}}'\"" > echo > done > done var=VAR1 ${!var@Q}:'' ${!var[@]@Q}: '' ${VAR1@Q}:'' ${VAR1[@]@Q}: '' var=VAR2 ${!var@Q}:''zzz'' ${!var[@]@Q}: ''zzz'' ${VAR2@Q}:''zzz'' ${VAR2[@]@Q}: ''zzz'' var=VAR3 ${!var@Q}:'' ${!var[@]@Q}: '' ${VAR3@Q}:'' ${VAR3[@]@Q}: '' var=VAR4 ${!var@Q}:''zzz'' ${!var[@]@Q}: ''zzz'' ${VAR4@Q}:''zzz'' ${VAR4[@]@Q}: ''zzz'' var=VAR5 ${!var@Q}:'' ${!var[@]@Q}: '' ${VAR5@Q}:'' ${VAR5[@]@Q}: '' var=VAR6 ${!var@Q}:''zzz'' ${!var[@]@Q}: ''zzz'' ${VAR6@Q}:''zzz'' ${VAR6[@]@Q}: ''zzz'' var=VAR1 ${!var@E}:'' ${!var[@]@E}: '' ${VAR1@E}:'' ${VAR1[@]@E}: '' var=VAR2 ${!var@E}:'zzz' ${!var[@]@E}: 'zzz' ${VAR2@E}:'zzz' ${VAR2[@]@E}: 'zzz' var=VAR3 ${!var@E}:'' ${!var[@]@E}: '' ${VAR3@E}:'' ${VAR3[@]@E}: '' var=VAR4 ${!var@E}:'zzz' ${!var[@]@E}: 'zzz' ${VAR4@E}:'zzz' ${VAR4[@]@E}: 'zzz' var=VAR5 ${!var@E}:'' ${!var[@]@E}: '' ${VAR5@E}:'' ${VAR5[@]@E}: '' var=VAR6 ${!var@E}:'zzz' ${!var[@]@E}: 'zzz' ${VAR6@E}:'zzz' ${VAR6[@]@E}: 'zzz' var=VAR1 ${!var@P}:'' ${!var[@]@P}: '' ${VAR1@P}:'' ${VAR1[@]@P}: '' var=VAR2 ${!var@P}:'zzz' ${!var[@]@P}: 'zzz' ${VAR2@P}:'zzz' ${VAR2[@]@P}: 'zzz' var=VAR3 ${!var@P}:'' ${!var[@]@P}: '' ${VAR3@P}:'' ${VAR3[@]@P}: '' var=VAR4 ${!var@P}:'zzz' ${!var[@]@P}: 'zzz' ${VAR4@P}:'zzz' ${VAR4[@]@P}: 'zzz' var=VAR5 ${!var@P}:'' ${!var[@]@P}: '' ${VAR5@P}:'' ${VAR5[@]@P}: '' var=VAR6 ${!var@P}:'zzz' ${!var[@]@P}: 'zzz' ${VAR6@P}:'zzz' ${VAR6[@]@P}: 'zzz' var=VAR1 ${!var@A}:'' ${!var[@]@A}: '' ${VAR1@A}:'' ${VAR1[@]@A}: 'declare -rl VAR1=''' var=VAR2 ${!var@A}:'declare -rl VAR2='zzz'' ${!var[@]@A}: 'declare -rl VAR2='zzz'' ${VAR2@A}:'declare -rl VAR2='zzz'' ${VAR2[@]@A}: 'declare -rl VAR2='zzz'' var=VAR3 ${!var@A}:'' ${!var[@]@A}: '' ${VAR3@A}:'' ${VAR3[@]@A}: '' var=VAR4 ${!var@A}:'declare -arl VAR4='zzz'' ${!var[@]@A}: 'declare -arl VAR4='zzz'' ${VAR4@A}:'declare -arl VAR4='zzz'' ${VAR4[@]@A}: 'declare -arl VAR4=([0]="zzz")' var=VAR5 ${!var@A}:'' ${!var[@]@A}: '' ${VAR5@A}:'' ${VAR5[@]@A}: '' var=VAR6 ${!var@A}:'declare -Arl VAR6='zzz'' ${!var[@]@A}: 'declare -Arl VAR6='zzz'' ${VAR6@A}:'declare -Arl VAR6='zzz'' ${VAR6[@]@A}: 'declare -Arl VAR6=([0]="zzz" )' var=VAR1 ${!var@a}:'' ${!var[@]@a}: '' ${VAR1@a}:'rl' ${VAR1[@]@a}: 'rl' var=VAR2 ${!var@a}:'rl' ${!var[@]@a}: 'rl' ${VAR2@a}:'rl' ${VAR2[@]@a}: 'rl' var=VAR3 ${!var@a}:'' ${!var[@]@a}: '' ${VAR3@a}:'arl' ${VAR3[@]@a}: '' var=VAR4 ${!var@a}:'arl' ${!var[@]@a}: 'arl' ${VAR4@a}:'arl' ${VAR4[@]@a}: 'arl' var=VAR5 ${!var@a}:'' ${!var[@]@a}: '' ${VAR5@a}:'Arl' ${VAR5[@]@a}: '' var=VAR6 ${!var@a}:'Arl' ${!var[@]@a}: 'Arl' ${VAR6@a}:'Arl' ${VAR6[@]@a}: 'Arl' $ -- Arfrever Frehtes Taifersar Arahesis
Re: ${!variable@operator} does not work for variables without values; inconsistencies between present and absent [@] for @A and @a
> ${variable@A} does not work for scalar variables without values, but > interestingly ${variable[@]@A} works for them. More precisely, ${variable[@]@A} is non-empty, but not exactly correct. > See difference between ${VAR1@A} and ${VAR1[@]@A} below. ${VAR1[@]@A} is: declare -rl VAR1=''" But should be: declare -rl VAR1 As in output of 'declare -p VAR1'. -- Arfrever Frehtes Taifersar Arahesis
${!variable@operator} does not work for variables without values; inconsistencies between present and absent [@] for @A and @a
${!variable@operator} does not work for variables without values. See empty values for all occurrences of ${!var@...} below. ${variable@A} does not work for scalar variables without values, but interestingly ${variable[@]@A} works for them. See difference between ${VAR1@A} and ${VAR1[@]@A} below. However neither ${variable@A} nor ${variable[@]@A} works for arrays without values. Both ${variable@a} and ${variable[@]@a} work for scalar variables without values. However ${variable[@]@a} does not work for arrays without values. See difference between ${VAR3@a} and ${VAR3[@]@a}, and between ${VAR5@a} and ${VAR5[@]@a} below. (BASH 5.0.16.) $ declare -lr VAR1 $ declare -lr VAR2=zzz $ declare -alr VAR3 $ declare -alr VAR4=(zzz) $ declare -Alr VAR5 $ declare -Alr VAR6=([0]=zzz) $ declare -p VAR{1,2,3,4,5,6} declare -rl VAR1 declare -rl VAR2="zzz" declare -arl VAR3 declare -arl VAR4=([0]="zzz") declare -Arl VAR5 declare -Arl VAR6=([0]="zzz" ) $ for operator in Q E P A a ; do for var in VAR{1,2,3,4,5,6} ; do echo "var=${var}" ; eval "echo \"\\\${!var@${operator}}: '\${!${var}@${operator}}'\"" ; eval "echo \"\\\${!var[@]@${operator}}: '\${!${var}[@]@${operator}}'\"" ; eval "echo \"\\\${${var}@${operator}}:'\${${var}@${operator}}'\"" ; eval "echo \"\\\${${var}[@]@${operator}}: '\${${var}[@]@${operator}}'\"" ; echo ; done ; done var=VAR1 ${!var@Q}:'' ${!var[@]@Q}: '' ${VAR1@Q}:'' ${VAR1[@]@Q}: '' var=VAR2 ${!var@Q}:'' ${!var[@]@Q}: '' ${VAR2@Q}:''zzz'' ${VAR2[@]@Q}: ''zzz'' var=VAR3 ${!var@Q}:'' ${!var[@]@Q}: '' ${VAR3@Q}:'' ${VAR3[@]@Q}: '' var=VAR4 ${!var@Q}:'' ${!var[@]@Q}: '' ${VAR4@Q}:''zzz'' ${VAR4[@]@Q}: ''zzz'' var=VAR5 ${!var@Q}:'' ${!var[@]@Q}: '' ${VAR5@Q}:'' ${VAR5[@]@Q}: '' var=VAR6 ${!var@Q}:'' ${!var[@]@Q}: '' ${VAR6@Q}:''zzz'' ${VAR6[@]@Q}: ''zzz'' var=VAR1 ${!var@E}:'' ${!var[@]@E}: '' ${VAR1@E}:'' ${VAR1[@]@E}: '' var=VAR2 ${!var@E}:'' ${!var[@]@E}: '' ${VAR2@E}:'zzz' ${VAR2[@]@E}: 'zzz' var=VAR3 ${!var@E}:'' ${!var[@]@E}: '' ${VAR3@E}:'' ${VAR3[@]@E}: '' var=VAR4 ${!var@E}:'' ${!var[@]@E}: '' ${VAR4@E}:'zzz' ${VAR4[@]@E}: 'zzz' var=VAR5 ${!var@E}:'' ${!var[@]@E}: '' ${VAR5@E}:'' ${VAR5[@]@E}: '' var=VAR6 ${!var@E}:'' ${!var[@]@E}: '' ${VAR6@E}:'zzz' ${VAR6[@]@E}: 'zzz' var=VAR1 ${!var@P}:'' ${!var[@]@P}: '' ${VAR1@P}:'' ${VAR1[@]@P}: '' var=VAR2 ${!var@P}:'' ${!var[@]@P}: '' ${VAR2@P}:'zzz' ${VAR2[@]@P}: 'zzz' var=VAR3 ${!var@P}:'' ${!var[@]@P}: '' ${VAR3@P}:'' ${VAR3[@]@P}: '' var=VAR4 ${!var@P}:'' ${!var[@]@P}: '' ${VAR4@P}:'zzz' ${VAR4[@]@P}: 'zzz' var=VAR5 ${!var@P}:'' ${!var[@]@P}: '' ${VAR5@P}:'' ${VAR5[@]@P}: '' var=VAR6 ${!var@P}:'' ${!var[@]@P}: '' ${VAR6@P}:'zzz' ${VAR6[@]@P}: 'zzz' var=VAR1 ${!var@A}:'' ${!var[@]@A}: '' ${VAR1@A}:'' ${VAR1[@]@A}: 'declare -rl VAR1=''' var=VAR2 ${!var@A}:'' ${!var[@]@A}: '' ${VAR2@A}:'declare -rl VAR2='zzz'' ${VAR2[@]@A}: 'declare -rl VAR2='zzz'' var=VAR3 ${!var@A}:'' ${!var[@]@A}: '' ${VAR3@A}:'' ${VAR3[@]@A}: '' var=VAR4 ${!var@A}:'' ${!var[@]@A}: '' ${VAR4@A}:'declare -arl VAR4='zzz'' ${VAR4[@]@A}: 'declare -arl VAR4=([0]="zzz")' var=VAR5 ${!var@A}:'' ${!var[@]@A}: '' ${VAR5@A}:'' ${VAR5[@]@A}: '' var=VAR6 ${!var@A}:'' ${!var[@]@A}: '' ${VAR6@A}:'declare -Arl VAR6='zzz'' ${VAR6[@]@A}: 'declare -Arl VAR6=([0]="zzz" )' var=VAR1 ${!var@a}:'' ${!var[@]@a}: '' ${VAR1@a}:'rl' ${VAR1[@]@a}: 'rl' var=VAR2 ${!var@a}:'' ${!var[@]@a}: '' ${VAR2@a}:'rl' ${VAR2[@]@a}: 'rl' var=VAR3 ${!var@a}:'' ${!var[@]@a}: '' ${VAR3@a}:'arl' ${VAR3[@]@a}: '' var=VAR4 ${!var@a}:'' ${!var[@]@a}: '' ${VAR4@a}:'arl' ${VAR4[@]@a}: 'arl' var=VAR5 ${!var@a}:'' ${!var[@]@a}: '' ${VAR5@a}:'Arl' ${VAR5[@]@a}: '' var=VAR6 ${!var@a}:'' ${!var[@]@a}: '' ${VAR6@a}:'Arl' ${VAR6[@]@a}: 'Arl' $ -- Arfrever Frehtes Taifersar Arahesis