help-bash
[Top][All Lists]
Advanced

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

Re: is this a rightful error ? i thought i can flat string much to decla


From: alex xmb ratchev
Subject: Re: is this a rightful error ? i thought i can flat string much to declare , but it seems ..
Date: Tue, 11 Apr 2023 17:12:19 +0200

thank you mate for answering

On Tue, Apr 11, 2023, 13:42 Greg Wooledge <greg@wooledge.org> wrote:

> On Tue, Apr 11, 2023 at 12:05:17PM +0200, alex xmb ratchev wrote:
> > $ var='a=( 1 2 3 ) b=( 4 5 5 )' ; declare -a "$var" ; declare -p a b
> > bash: syntax error near unexpected token `)'
>
> It would be helpful to see *which* of the three commands is actually
> causing the error, by running them separately.
>
> unicorn:~$ var='a=( 1 2 3 ) b=( 4 5 5 )'
> unicorn:~$ unset a b
> unicorn:~$ declare -a "$var"
> bash: syntax error near unexpected token `)'
> unicorn:~$ declare -p a b
> declare -a a=()
> bash: declare: b: not found
>
> So, the command which gives the error is:  declare -a "$var"
>
> My reading of the situation is that declare is seeing a string argument
> that begins with a=( and ends with ), so it assumes that you're giving it
> a single array definition for the variable "a", and that each of the
> words inside the parentheses should be parsed and expanded to become
> the elements of that array.
>
> The parser fails when it hits the fourth word ")".
>
> One may argue whether this behavior is right or wrong, but I find it
> surprising that you expected this to work.  You're passing TWO separate
> array variable definitions in one string argument.
>

i .. had some memory thought it ' used to work '

placing em individually is the right solution , yes

I would pass them separately.
>
> unicorn:~$ a1='a=(1 2 3)' a2='b=(4 5 5)'
> unicorn:~$ declare -a "$a1" "$a2"
> unicorn:~$ declare -p a b
> declare -a a=([0]="1" [1]="2" [2]="3")
> declare -a b=([0]="4" [1]="5" [2]="5")
>
> By the way, you might be operating under the false assumption that this
> is somehow safer than using eval, because you spelled it differently.
> It's not any safer.
>

uh .. :/

unicorn:~$ a1='a=(1 2 `date`)'
> unicorn:~$ declare -a "$a1"
> unicorn:~$ declare -p a
> declare -a a=([0]="1" [1]="2" [2]="Tue" [3]="Apr" [4]="11" [5]="07:36:20"
> [6]="EDT" [7]="2023")
>

well as for theoretical .. i would @Q some values .. somewhere ..

In fact, if you stop being all fancy, and just use eval in the first
> place...
>

<long text skipped..>

unicorn:~$ var='a=( 1 2 3 ) b=( 4 5 5 )'
> unicorn:~$ eval "$var"
> unicorn:~$ declare -p a b
> declare -a a=([0]="1" [1]="2" [2]="3")
> declare -a b=([0]="4" [1]="5" [2]="5")
>
> ... it actually works BETTER.
>

yea it looks chique

thanks ..
greets

>


reply via email to

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