Skip to main content
deleted 297 characters in body; edited title
Source Link
muru
  • 77.9k
  • 16
  • 212
  • 318

I don't understand How does arithmetic expansion work with variblesvariables without the $ (dollar) sign?

I think the line below should be written differently, and the command should bug out.

$ echo $((x=2, y=3, x+y))
5

I thought the way to write this was:

$ echo $((x=2, y=3, $x+$y))
5

Both work, which now confuses me. I'm assuming I have a mis-understanding somewhere, so I will explain how I decided the first echo is wrong. The x+y is not referencing a variable, so how could bash possibly know that x and y are a variable. If you tell me it's because of the $ at the start of the expression, then you must be wrong because x and y would have been defined at $x=2 and $y=2 which would be an incorrect way to define a variable. So I have absolutely no idea how bash can know that x and y are varaibles.

Whoever marked this as duplicate, should unmark it as duplicate. It has an error in that the other post did not. It also got an answer that is far superior to the one in the linked post, and it will be far better to use the answer on my post as the de-factor for the answer to this questionvariables.

I don't understand arithmetic expansion with varibles

I think the line below should be written differently, and the command should bug out.

$ echo $((x=2, y=3, x+y))
5

I thought the way to write this was:

$ echo $((x=2, y=3, $x+$y))
5

Both work, which now confuses me. I'm assuming I have a mis-understanding somewhere, so I will explain how I decided the first echo is wrong. The x+y is not referencing a variable, so how could bash possibly know that x and y are a variable. If you tell me it's because of the $ at the start of the expression, then you must be wrong because x and y would have been defined at $x=2 and $y=2 which would be an incorrect way to define a variable. So I have absolutely no idea how bash can know that x and y are varaibles.

Whoever marked this as duplicate, should unmark it as duplicate. It has an error in that the other post did not. It also got an answer that is far superior to the one in the linked post, and it will be far better to use the answer on my post as the de-factor for the answer to this question.

How does arithmetic expansion work with variables without the $ (dollar) sign?

I think the line below should be written differently, and the command should bug out.

$ echo $((x=2, y=3, x+y))
5

I thought the way to write this was:

$ echo $((x=2, y=3, $x+$y))
5

Both work, which now confuses me. I'm assuming I have a mis-understanding somewhere, so I will explain how I decided the first echo is wrong. The x+y is not referencing a variable, so how could bash possibly know that x and y are a variable. If you tell me it's because of the $ at the start of the expression, then you must be wrong because x and y would have been defined at $x=2 and $y=2 which would be an incorrect way to define a variable. So I have absolutely no idea how bash can know that x and y are variables.

Became Hot Network Question
Post Reopened by cas bash
added 297 characters in body
Source Link
john smith
  • 895
  • 6
  • 17
  • 32

I think the line below should be written differently, and the command should bug out.

$ echo $((x=2, y=3, x+y))
5

I thought the way to write this was:

$ echo $((x=2, y=3, $x+$y))
5

Both work, which now confuses me. I'm assuming I have a mis-understanding somewhere, so I will explain how I decided the first echo is wrong. The x+y is not referencing a variable, so how could bash possibly know that x and y are a variable. If you tell me it's because of the $ at the start of the expression, then you must be wrong because x and y would have been defined at $x=2 and $y=2 which would be an incorrect way to define a variable. So I have absolutely no idea how bash can know that x and y are varaibles.

Whoever marked this as duplicate, should unmark it as duplicate. It has an error in that the other post did not. It also got an answer that is far superior to the one in the linked post, and it will be far better to use the answer on my post as the de-factor for the answer to this question.

I think the line below should be written differently, and the command should bug out.

$ echo $((x=2, y=3, x+y))
5

I thought the way to write this was:

$ echo $((x=2, y=3, $x+$y))
5

Both work, which now confuses me. I'm assuming I have a mis-understanding somewhere, so I will explain how I decided the first echo is wrong. The x+y is not referencing a variable, so how could bash possibly know that x and y are a variable. If you tell me it's because of the $ at the start of the expression, then you must be wrong because x and y would have been defined at $x=2 and $y=2 which would be an incorrect way to define a variable. So I have absolutely no idea how bash can know that x and y are varaibles.

I think the line below should be written differently, and the command should bug out.

$ echo $((x=2, y=3, x+y))
5

I thought the way to write this was:

$ echo $((x=2, y=3, $x+$y))
5

Both work, which now confuses me. I'm assuming I have a mis-understanding somewhere, so I will explain how I decided the first echo is wrong. The x+y is not referencing a variable, so how could bash possibly know that x and y are a variable. If you tell me it's because of the $ at the start of the expression, then you must be wrong because x and y would have been defined at $x=2 and $y=2 which would be an incorrect way to define a variable. So I have absolutely no idea how bash can know that x and y are varaibles.

Whoever marked this as duplicate, should unmark it as duplicate. It has an error in that the other post did not. It also got an answer that is far superior to the one in the linked post, and it will be far better to use the answer on my post as the de-factor for the answer to this question.

Post Closed as "Duplicate" by muru bash
Source Link
john smith
  • 895
  • 6
  • 17
  • 32

I don't understand arithmetic expansion with varibles

I think the line below should be written differently, and the command should bug out.

$ echo $((x=2, y=3, x+y))
5

I thought the way to write this was:

$ echo $((x=2, y=3, $x+$y))
5

Both work, which now confuses me. I'm assuming I have a mis-understanding somewhere, so I will explain how I decided the first echo is wrong. The x+y is not referencing a variable, so how could bash possibly know that x and y are a variable. If you tell me it's because of the $ at the start of the expression, then you must be wrong because x and y would have been defined at $x=2 and $y=2 which would be an incorrect way to define a variable. So I have absolutely no idea how bash can know that x and y are varaibles.