Shell gotcha

Today I was puzzled as I found out that the bash one-liner A=foo echo $A does not print 'foo'.

The answer came thanks to Vorlon and Wiggy: A=foo echo sets A in the environment of the echo command, and echo $A passes the value of $A in the shell as an argument to echo. $A is expanded before the line is run and at that time A=foo hasn't been processed yet.

One quick fix is doing A=foo; echo $A, but this leaves A set to foo after the execution of the command. To make it so that A is foo only when executing echo, one can use parenthesis: (A=foo ; echo $A):

$ (A=foo; echo $A); echo $A


(note: the boring presence of "foo" in these examples should be taken as a reminder of my call for no-op words)