It doesn't matter that echo
is a built-in. The reason is that variables on the command line are evaluated before executing the command, they're not evaluated by the command. You could get the result you want with:
FOO=42 bash -c 'echo "$FOO"'
This starts a new shell to execute echo $foo
. Since the argument to bash
is in single quotes, the variable is not replaced by the original shell.