help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Chain/cascade assignment


From: Greg Wooledge
Subject: Re: [Help-bash] Chain/cascade assignment
Date: Mon, 22 Feb 2016 08:21:59 -0500
User-agent: Mutt/1.4.2.3i

On Sun, Feb 21, 2016 at 11:35:52PM +0300, ?????????????? ???????????? wrote:
> search results and simple test in CLI show, that there is no chain 
> assignment in bash like var=var=val. I wonder why and want to propose 
> this as a feature (as far this assign construction seems common in other 
> languages).

Adding this to bash would break compatibility with existing scripts.
Given the bash command:

a1=a2=a3=a4=a5=42

The result is a single variable a1 with the value "a2=a3=a4=a5=42".

However, if you want a limited version of this, that only works with
integer values, you can use the C-style arithmetic command:

imadev:~$ ((a1=a2=a3=a4=a5=42))
imadev:~$ declare -p a1 a2 a3 a4 a5
declare -- a1="42"
declare -- a2="42"
declare -- a3="42"
declare -- a4="42"
declare -- a5="42"

I'm not sure what your actual goal is.  The only time I can really see
a use for this is initializing a lot of variables to empty/zero.  If
they are numeric and you want to initialize them to 0, then you can
use the ((...)) form I just showed.

If you want to initialize a bunch of variables to empty values, it's
cleaner to use unset:

unset a1 a2 a3 a4 a5

Yes, they are not actually "initialized to empty values" here, but for
all practical purposes you get the same result.  (Note: users of set -u
are not practical, so they don't count.)

The other way, which actually initializes them, would be:

a1= a2= a3= a4= a5=

That's only slightly more awkward to write.

In short, I don't see a compelling need for this proposed feature.



reply via email to

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