LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   script interpreting - "let" (https://www.linuxquestions.org/questions/linux-newbie-8/script-interpreting-let-4175583355/)

vincix 06-29-2016 11:22 AM

script interpreting - "let"
 
I'm currently reading this:
http://www.tldp.org/LDP/abs/abs-guide.pdf

What is the meaning of the following?
let "t2 = ((a = 9, 15 / 3))"
# Set "a = 9" and "t2 = 15 / 3"

I don't understand the relationship between t2 and a here. Is the answer in the comment correct?

pan64 06-29-2016 11:31 AM

Code:

echo $a
- and -
echo $t2

will tell you the answer

vincix 06-29-2016 11:38 AM

Well, ok, it's correct, but that wasn't actually the question, obviously. I don't understand why this is happening.

rknichols 06-29-2016 12:36 PM

"a = 9" is an expression with the side effect of setting the value of variable a. It returns that value.

"," is an operator that evaluates both of its operands and returns the value of its second operand.

millgates 06-29-2016 12:36 PM

You need to start reading the expression from inside the parentheses:

Code:

a = 9
Assigns 9 to a, the result of the assignment being the value assigned.
The result of
Code:

15 / 3
is 5. So, we get
Code:

let "t2 = ((9, 5))"
Now, the comma operator works like this: it discards its first operand and returns the other:
Code:

let "t2 = ((5))"
and the '5' gets assigned to t2.

pan64 06-29-2016 01:49 PM

This is a quite unusual case. I mean this is not the way how it was planned to use. Especially the , (comma) operator. Which is actually uncommon/unusual.
here you can find a bit more: http://unix.stackexchange.com/questi...ash-arithmetic

vincix 06-29-2016 02:23 PM

Quote:

Originally Posted by millgates (Post 5568022)
Now, the comma operator works like this: it discards its first operand and returns the other:

Indeed, that's exactly the explanation I was looking for. But as pan64 points it, it's rather weird, at least in this case.
Thank you for the explanations and references.


All times are GMT -5. The time now is 11:52 PM.