[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Some incorrect behaviour for BASH arrays
From: |
Kerin Millar |
Subject: |
Re: Some incorrect behaviour for BASH arrays |
Date: |
Thu, 31 Aug 2023 14:50:31 +0100 |
On Thu, 31 Aug 2023 19:39:09 +0700
Victor Pasko <victor.pasko@gmail.com> wrote:
> Thanks for prompt response. But see my comments below.
>
> Well, instead of the following line
>
> RESULT=''
>
> done
>
> declare -p RESULT
>
> without initialization, because otherwise I got the following error "-bash:
> declare: RESULT=: not found".
declare -p RESULT='' translates as declare -p RESULT=, which instructs bash to
attempt to dump the contents of a variable named RESULT=. There is no variable
named RESULT=, so it prints an error to that effect. Put another way, you're
not supposed to be using declare -p as a substitute for assignment syntax.
While declare can also be used to set variables, that's not what the -p option
is for.
>
> And after deleting/commenting the following fragment in badrev
>
> #if [ $i -ge $QSTRING ] ; then
> #echo "XXX-break i=$i result=$RESULT size=${#RESULT}"
> #break
> #fi
>
> and correcting end echo-line to
>
> echo " result=${RESULT[0]} size=${#RESULT[0]}"
${RESULT[0]} will expand as the first element of the array. ${#RESULT[0]} will
expand as the length of the first element of the array (not the length of the
array itself).
>
> i can see strange output for badrev:
> declare -a RESULT=([0]="987654321" [1]="B" [2]="1" [3]="2" [4]="3" [5]="\\"
> [6]="-" [7]="4" [8]="5" [9]="6" [10]="7" [11]="\\" [12]="-" [13]="8")
> result=9 size=1
> Still incorrect output and size:( How to get correct output? ${RESULT[0]}
> ???
What would be correct to you?
>
> result=${RESULT[0]} size=${#RESULT[0]} ### does not work :(
What would it be doing for you to be satisfied that it works?
Your script contains no explanation as to what it is intended to do. Comments
to the effect of "not working" do not help to discern your intent.
>
> BTW, in the presented link to the manual with *6.7 Arrays*
> there is no explanation for *declare -p *:(
It's a builtin command, so it's listed within the SHELL BUILTIN COMMANDS
section. Also, you may use the help builtin to display some (less detailed)
documentation.
$ help declare | grep -- -p
-p display the attributes and value of each NAME
--
Kerin Millar