When you use
LANG=C gcc ... what happens is that the shell sets LANG for
gcc's environment only, and not for the current environment itself (see note). So after
LANG is back to its previous value (or unset).
Additionally, when you use
A=10 echo $A it is the shell that replaces $A, not echo, and this substitution (called "expansion") happens before the statement is evaluated (including the assignment), so to work as expected
A's value must be already set in the current environment prior to that statement.
A=10 echo $A does not work as expected:
A=10 will be set for echo, but echo internally ignores the value of the environment variable
$A is replaced with the value set in the current shell (which is none), and then passed as an argument to echo.
So your assumption is correct:
VAR=value command does work, but this is only relevant if
command internally uses VAR. If not, you can still pass
value as an argument to
command, but arguments are replaced by the current shell, so they must be set prior to usage:
VAR=value; command "$VAR"
If you know how to create an executable script, you can try this as a test:
echo "1st argument is $1"
echo "A is $A"
Save it as
testscript and try:
$ A=5; A=10 testscript "$A"; echo "$A"
1st argument is 5
A is 10
Last but not least, it's worth knowing the difference between shell and environment variables and program arguments.
Here are some good references:
(*) Note: technically the shell does set in current environment too, and here's why: Some commands, like
test are shell builtins, and as such they don't spawn a child process. They run in current environment. But the shell takes care for the assignment only lasts until the command is running, so for all practical purposes the effect is the same: the assignment is only seen by that single command.