Re: No word splitting for assignment-like expressions in compound assignment

2020-07-28 Thread Chet Ramey
On 7/28/20 4:14 PM, Ilkka Virta wrote:
> On 28.7. 17:22, Chet Ramey wrote:
>> On 7/23/20 8:11 PM, Alexey Izbyshev wrote:
>>> $ Z='a b'
>>> $ A=(X=$Z)
>>> $ declare -p A
>>> declare -a A=([0]="X=a b")
> 
>> It's an assignment statement in a context where assignment statements are
>> accepted (which is what makes it different from `echo X=$Z', for instance),
>> but the lack of a subscript on the lhs makes it a special case. I'll take a
>> look at the semantics here.
> 
> This is also a bit curious:
> 
>  $ b=( [123]={a,b,c}x )
>  $ declare -p b
>  declare -a b=([0]="[123]=ax" [1]="[123]=bx" [2]="[123]=cx")
> 
> It does seem to have a subscript on the LHS, but it didn't work as one.
> To be in line with a plain scalar assignment, the braces should probably
> not be expanded here.

Thanks, I'll look at it.


-- 
``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: No word splitting for assignment-like expressions in compound assignment

2020-07-28 Thread Ilkka Virta

On 28.7. 17:22, Chet Ramey wrote:

On 7/23/20 8:11 PM, Alexey Izbyshev wrote:

$ Z='a b'
$ A=(X=$Z)
$ declare -p A
declare -a A=([0]="X=a b")



It's an assignment statement in a context where assignment statements are
accepted (which is what makes it different from `echo X=$Z', for instance),
but the lack of a subscript on the lhs makes it a special case. I'll take a
look at the semantics here.


This is also a bit curious:

 $ b=( [123]={a,b,c}x )
 $ declare -p b
 declare -a b=([0]="[123]=ax" [1]="[123]=bx" [2]="[123]=cx")

It does seem to have a subscript on the LHS, but it didn't work as one.
To be in line with a plain scalar assignment, the braces should probably 
not be expanded here.



--
Ilkka Virta / itvi...@iki.fi



Re: No word splitting for assignment-like expressions in compound assignment

2020-07-28 Thread Chet Ramey
On 7/23/20 8:11 PM, Alexey Izbyshev wrote:
> Hello!
> 
> I have a question about the following behavior:
> 
> $ Z='a b'
> $ A=(X=$Z)
> $ declare -p A
> declare -a A=([0]="X=a b")
> $ A=(X$Z)
> $ declare -p A
> declare -a A=([0]="Xa" [1]="b")
> 
> I find it surprising that no word splitting is performed in the first
> compound assignment. I realize that skipping word splitting may be
> desirable if a subscript is given (e.g. "A=([0]=$Z)") to make it consistent
> with normal variable assignment[1], but in this case it looks like a bug.

It's an assignment statement in a context where assignment statements are
accepted (which is what makes it different from `echo X=$Z', for instance),
but the lack of a subscript on the lhs makes it a special case. I'll take a
look at the semantics here.

-- 
``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: No word splitting for assignment-like expressions in compound assignment

2020-07-27 Thread Lawrence Velázquez
> On Jul 27, 2020, at 4:02 AM, Andreas Schwab  wrote:
> 
> On Jul 27 2020, Lawrence Velázquez wrote:
> 
>> If word splitting were not performed in compound assignments, this...
>> 
>>foo=(a b c)
>> 
>> ...would not work.
> 
> This is not true.  Field splitting is only relevant for words generated
> by other expansions, not for literal tokens.

My fault, I got it mixed up with tokenization. Carry on!

vq


Re: No word splitting for assignment-like expressions in compound assignment

2020-07-27 Thread Greg Wooledge
On Mon, Jul 27, 2020 at 01:31:32AM -0400, Dale R. Worley wrote:
> So it seems like the word splitting in "A=(X$Z)" is incorrect.

If the documentation doesn't support word splitting in that case,
then it's the documentation that will need to change, not the shell.

Word splitting in that context is *frequently* used in scripts.  Never
mind the fact that it's dangerous and wrong -- people use it, a lot.
A change to the shell that would stop it from occurring would break
countless scripts.



Re: No word splitting for assignment-like expressions in compound assignment

2020-07-27 Thread Oğuz
27 Temmuz 2020 Pazartesi tarihinde Alexey Izbyshev 
yazdı:

> On 2020-07-27 10:06, Lawrence Velázquez wrote:
>
>> On Jul 27, 2020, at 1:31 AM, Dale R. Worley  wrote:
>>> Interesting.  The documentation for 4.2.53(1) says this about parameter
>>> assignments generally, with no special rules for compound assignments:
>>>
>>>   All
>>>   values undergo tilde expansion, parameter and variable expansion,
>>> com-
>>>   mand  substitution, arithmetic expansion, and quote removal (see
>>> EXPAN-
>>>   SION below).  ...  Word  splitting  is  not
>>>   performed,  with the exception of "$@" as explained below under
>>> Special
>>>   Parameters.  Pathname expansion is not  performed.
>>>
>>> So it seems like the word splitting in "A=(X$Z)" is incorrect.  So is
>>> pathname expansion in that context.
>>>
>>
>>
>> If word splitting were not performed in compound assignments, this...
>>
>> foo=(a b c)
>>
>> ...would not work. If pathname expansion were not performed in compound
>> assignments, this...
>>
>> foo=(*)
>>
>> ...would not work. Arrays would become significantly less usable if
>> word splitting and pathname expansion were not allowed in compound
>> assignments.
>>
>> To be clear, I don't consider word splitting and expansions in compound
> assignments to be a problem: this is well-known and long-standing behavior,
> even though it doesn't seem to be explicitly documented. In particular, I
> expect word splitting to happen in "A=(X$Z)" case. But I expect it to
> happen in "A=(X=$Z)" too, and the lack of it seems unintentional to me.


I agree, anything that forms a valid assignment statement in isolation is
exempt from word splitting and that indeed seems like a bug or very poor
implementation choice.

$ Z='a b'
$ A=(X=$Z X[123]=$Z X[qwerty]=$Z X+=$Z)
$ declare -p A
declare -a A=([0]="X=a b" [1]="X[123]=a b" [2]="X[qwerty]=a b"
[3]="X+=a b")


>
> Alexey
>
>

-- 
Oğuz


Re: No word splitting for assignment-like expressions in compound assignment

2020-07-27 Thread Andreas Schwab
On Jul 27 2020, Lawrence Velázquez wrote:

> If word splitting were not performed in compound assignments, this...
>
> foo=(a b c)
>
> ...would not work.

This is not true.  Field splitting is only relevant for words generated
by other expansions, not for literal tokens.

Andreas.

-- 
Andreas Schwab, sch...@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."



Re: No word splitting for assignment-like expressions in compound assignment

2020-07-27 Thread Alexey Izbyshev

On 2020-07-27 10:06, Lawrence Velázquez wrote:
On Jul 27, 2020, at 1:31 AM, Dale R. Worley  
wrote:
Interesting.  The documentation for 4.2.53(1) says this about 
parameter

assignments generally, with no special rules for compound assignments:

  All
  values undergo tilde expansion, parameter and variable 
expansion,  com-
  mand  substitution, arithmetic expansion, and quote removal (see 
EXPAN-

  SION below).  ...  Word  splitting  is  not
  performed,  with the exception of "$@" as explained below under 
Special

  Parameters.  Pathname expansion is not  performed.

So it seems like the word splitting in "A=(X$Z)" is incorrect.  So is
pathname expansion in that context.



If word splitting were not performed in compound assignments, this...

foo=(a b c)

...would not work. If pathname expansion were not performed in compound
assignments, this...

foo=(*)

...would not work. Arrays would become significantly less usable if
word splitting and pathname expansion were not allowed in compound
assignments.

To be clear, I don't consider word splitting and expansions in compound 
assignments to be a problem: this is well-known and long-standing 
behavior, even though it doesn't seem to be explicitly documented. In 
particular, I expect word splitting to happen in "A=(X$Z)" case. But I 
expect it to happen in "A=(X=$Z)" too, and the lack of it seems 
unintentional to me.


Alexey



Re: No word splitting for assignment-like expressions in compound assignment

2020-07-27 Thread Lawrence Velázquez
> On Jul 27, 2020, at 1:31 AM, Dale R. Worley  wrote:
> 
> Alexey Izbyshev  writes:
>> I have a question about the following behavior:
>> 
>> $ Z='a b'
>> $ A=(X=$Z)
>> $ declare -p A
>> declare -a A=([0]="X=a b")
>> $ A=(X$Z)
>> $ declare -p A
>> declare -a A=([0]="Xa" [1]="b")
>> 
>> I find it surprising that no word splitting is performed in the first 
>> compound assignment.
> 
>> * Brace expansion is performed for "A=(X=a{x,y}b)" by all bash versions 
>> mentioned above (which is inconsistent with normal variable assignment).
>> * Globbing for "A=(X=a?b)" is performed by bash 3.1.17, but not by other 
>> versions.
> 
> Interesting.  The documentation for 4.2.53(1) says this about parameter
> assignments generally, with no special rules for compound assignments:
> 
>   All
>   values undergo tilde expansion, parameter and variable expansion,  com-
>   mand  substitution, arithmetic expansion, and quote removal (see EXPAN-
>   SION below).  ...  Word  splitting  is  not
>   performed,  with the exception of "$@" as explained below under Special
>   Parameters.  Pathname expansion is not  performed.
> 
> So it seems like the word splitting in "A=(X$Z)" is incorrect.  So is
> pathname expansion in that context.


If word splitting were not performed in compound assignments, this...

foo=(a b c)

...would not work. If pathname expansion were not performed in compound
assignments, this...

foo=(*)

...would not work. Arrays would become significantly less usable if
word splitting and pathname expansion were not allowed in compound
assignments.

vq


Re: No word splitting for assignment-like expressions in compound assignment

2020-07-26 Thread Dale R. Worley
Alexey Izbyshev  writes:
> I have a question about the following behavior:
>
> $ Z='a b'
> $ A=(X=$Z)
> $ declare -p A
> declare -a A=([0]="X=a b")
> $ A=(X$Z)
> $ declare -p A
> declare -a A=([0]="Xa" [1]="b")
>
> I find it surprising that no word splitting is performed in the first 
> compound assignment.

> * Brace expansion is performed for "A=(X=a{x,y}b)" by all bash versions 
> mentioned above (which is inconsistent with normal variable assignment).
> * Globbing for "A=(X=a?b)" is performed by bash 3.1.17, but not by other 
> versions.

Interesting.  The documentation for 4.2.53(1) says this about parameter
assignments generally, with no special rules for compound assignments:

   All
   values undergo tilde expansion, parameter and variable expansion,  com-
   mand  substitution, arithmetic expansion, and quote removal (see EXPAN-
   SION below).  ...  Word  splitting  is  not
   performed,  with the exception of "$@" as explained below under Special
   Parameters.  Pathname expansion is not  performed.

So it seems like the word splitting in "A=(X$Z)" is incorrect.  So is
pathname expansion in that context.  Oddly, brace expansion is not
mentioned.

Dale



No word splitting for assignment-like expressions in compound assignment

2020-07-23 Thread Alexey Izbyshev

Hello!

I have a question about the following behavior:

$ Z='a b'
$ A=(X=$Z)
$ declare -p A
declare -a A=([0]="X=a b")
$ A=(X$Z)
$ declare -p A
declare -a A=([0]="Xa" [1]="b")

I find it surprising that no word splitting is performed in the first 
compound assignment. I realize that skipping word splitting may be 
desirable if a subscript is given (e.g. "A=([0]=$Z)") to make it 
consistent with normal variable assignment[1], but in this case it looks 
like a bug.


I've reproduced the described behavior on bash 4.4.20 (Ubuntu 18.04), 
3.1.17 (CentOS 5) and self-built 5.1-alpha release.


Some other discoveries:
* Brace expansion is performed for "A=(X=a{x,y}b)" by all bash versions 
mentioned above (which is inconsistent with normal variable assignment).
* Globbing for "A=(X=a?b)" is performed by bash 3.1.17, but not by other 
versions.


[1] https://lists.gnu.org/archive/html/bug-bash/2012-08/msg00055.html

Alexey