Assignment visible only to the forked processSH-2097
Problematic code:
name=World cmd -m "Hello $name"
Correct code:
name=World
cmd -m "Hello $name"
If the original goal was to limit the scope of the variable, this can also be done in a subshell:
(
name=World
cmd -m "Hello $name"
) # 'name' does not leave this subshell
Rationale:
In name=World cmd "$name"
, name=World
is passed in as part of the environment to cmd
(i.e., in the envp
parameter to execve(2)).
This means that cmd
and its children will see the parameter, but other processes won't.
However, "$name"
is not expanded by cmd
. "$name"
is expanded by the shell before cmd
is ever executed, and thus it will not use the new value.
The solution is to set the variable first, then use it as a parameter. If limited scope is desired, a ( subshell )
can be used.
Exceptions
In the strange and fabricated scenarios where the program concerned uses that specific identifier as an environment variable for its own purposes, you can ignore this message. This is not good practice on the program's part, since scripts use lowercase variable names specifically to avoid collisions with environment variable definitions.