4

If I understand correctly, the syntax

Var=<something> command 

should run the command after setting Var to "something". Then why does "A=3; A=4 echo $A" produces 3 in my bash?

Shloim
  • 5,281
  • 21
  • 36
zell
  • 9,830
  • 10
  • 62
  • 115

2 Answers2

7

Variables in bash are evaluated before execution starts and not during execution, so we have a preprocessing stage for the command:

A=4 echo $A

$A is evaluated to the current value of A and replaces it before the execution to:

A=4 echo 3

and only then it is executed, A changes value to 4, and 3 is printed.

Shloim
  • 5,281
  • 21
  • 36
0

This is because you do not put a semicolon after the second variable assignment.

Try the following instead:

A=3; A=4; echo $A
iwita
  • 91
  • 6
  • 2
    While technically true, you are describing a different effect than what the OP is looking for. – hymie Nov 05 '19 at 13:37