Can someone explain to me the result of the following script in zsh:
#!/bin/zsh
var1=var2
var2=first
var3=second
echo var1="$var1"
echo var2="$var2"
echo var3="$var3"
echo '${${(P)var1::=var3}}'="${${(P)var1::=var3}}"
echo var1="$var1"
echo var2="$var2"
echo var3="$var3"
echo ------------
var1=var2
var2=first
var3=second
echo var1="$var1"
echo var2="$var2"
echo var3="$var3"
echo '${(P)var1::=var3}'="${(P)var1::=var3}"
echo var1="$var1"
echo var2="$var2"
echo var3="$var3"
Result:
var1=var2
var2=first
var3=second
${${(P)var1::=var3}}=second
var1=var2
var2=var3
var3=second
------------
var1=var2
var2=first
var3=second
${(P)var1::=var3}=var3
var1=var2
var2=var3
var3=second
As far as I understand the zsh manual (14.3.2 Rules), the execution of the first section (above ------) should be the following:
- Point 7 (apply the ::= operator) (set var1 to var3)
- Point 25 (apply the (P) operator) (dereference var1, get var3)
- Output the value of var3: 'second'
These steps agree with the following line in the result:
${${(P)var1::=var3}}=second
But why in the result the following lines appear:
var1=var2
var2=var3
instead of the expected:
var1=var3
var2=first
The execution of the second section (below ----) should be (according to the manual):
- Point 4 (apply the (P) operator) (dereference var1, get var2)
- Point 7 (apply the ::= operator) (set var2 to var3)
- Output the value of var2: 'var3'
The results of this section match the explanation above perfectly, so the problem is only with the first section.