I expected a to be something after the initial assignment. But I get nothing.

tomas@tomas-Latitude-E4200:~$ echo $a

tomas@tomas-Latitude-E4200:~$ a=0 echo $a


Similar unexpected behaviour when a is set before.

tomas@tomas-Latitude-E4200:~$ a=0
tomas@tomas-Latitude-E4200:~$ a=1 echo $a


marked as duplicate by Stéphane Chazelas bash Dec 1 '16 at 21:05

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Temporary environment variable assigments take effect during the execution of the command, not before. In particular, they are not yet effective during the parsing of the command line. Consider the following two commands for illustration:

$ A=1 sh -c "echo $A"

$ A=1 sh -c 'echo $A'

The difference between the example commands is that in the first command the variable substitutions happen before sh -c, and in the second they happen during the execution of sh -c.

Technically, the shell adds the temporary variable assignments to the environment of the child process; they are not added to the environment of the shell.

Not the answer you're looking for? Browse other questions tagged or ask your own question.