Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does "A=3; A=4 echo $A" produce 3 (instead of 4) in Bash? [duplicate]

Tags:

linux

bash

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?

like image 818
zell Avatar asked Mar 04 '23 05:03

zell


1 Answers

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.

like image 152
Shloim Avatar answered Apr 28 '23 07:04

Shloim