0

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

tomas@tomas-Latitude-E4200:~$

Similar unexpected behaviour when a is set before.

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

Well?

1 Answers1

3

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'
1

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.

AlexP
  • 10,455