What you see is the expected behaviour. The trouble is that the parent shell evaluates $SOMEVAR
on the command line before it invokes the command with the modified environment. You need to get the evaluation of $SOMEVAR
deferred until after the environment is set.
Your immediate options include:
SOMEVAR=BBB eval echo zzz '$SOMEVAR' zzz
.SOMEVAR=BBB sh -c 'echo zzz $SOMEVAR zzz'
.
Both these use single quotes to prevent the parent shell from evaluating $SOMEVAR
; it is only evaluated after it is set in the environment (temporarily, for the duration of the single command).
Another option is to use the sub-shell notation (as also suggested by Marcus Kuhn in his answer):
(SOMEVAR=BBB; echo zzz $SOMEVAR zzz)
The variable is set only in the sub-shell