2
$ A=123
$ echo $A  # now value of A is 123
123

$ A=456 echo a random following command
a random following command
$ echo $A  # we can see that A reminds 123 unchanged
123

Why it works different with and without following a command?
Any office bash doc link is helpful.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
mitnk
  • 3,127
  • 2
  • 21
  • 28
  • BTW, this is all behavior specified by POSIX.2 (and thus common to all standard-compliant shells), rather than specific to bash. – Charles Duffy Sep 16 '18 at 03:54

1 Answers1

8

This command:

A=456 echo a random following command

is similar to this command:

env A=456 echo a random following command

The new value 456 of A will only be used for the echo command. After the execution of the command echo, the variable A regains the original value.

The official document is Bash Reference Manual: Environment; as it explains:

The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters. These assignment statements affect only the environment seen by that command.


Update 0x00

When we run the following commands:

A=123
A=456 echo $A

The output is still 123, not 456. The reason is that Bash will evaluate $A first before executing the command A=456 echo $A. So the command:

A=456 echo $A

becomes

A=456 echo 123

And then we got the output 123.

But the following example is different

A=123
A=456 eval echo '$A'

This output is 456.

Because '$A' is a plain string, Bash will not evaluate it before running the command. When the eval command is running, the variable A is set to 456 by Bash.


Update 0x01

A subshell is also a child process, but a variable which is not marked for export is still available for the subshell.

Bash Reference Manual: Command Execution Environment, as it explains:

Command substitution, commands grouped with parentheses, and asynchronous commands are invoked in a subshell environment that is a duplicate of the shell environment

A=123
( eval echo \$A )

The output is 123. Even we put (eval echo \$A) into the background, the output is still 123.

A=123
( eval echo \$A )&

When we execute a simple command, not a built-in or shell function, such as executing a script. This script is invoked in a separated shell environment. Only the shell variables marked for export are available for the separated environment.

A=123
bash -c 'echo A1=$A'
export A
bash -c 'echo A2=$A'

The output is:

A1=
A2=123
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Feng
  • 3,592
  • 2
  • 15
  • 14
  • 2
    Thanks, this answered half of my question. I've read some text [here](https://ss64.com/bash/export.html), which explain the bare `A=123` (vs `export A=123`) part: `A=123` defines a shell variable, while a `export` make it a environment variable (which then can affected in child processes). – mitnk Sep 15 '18 at 12:46
  • 1
    Note the bullet point regarding questions which "*...have already been asked and answered many times before*" in [How to Answer](https://stackoverflow.com/help/how-to-answer). – Charles Duffy Sep 16 '18 at 03:10
  • @CharlesDuffy your edit (especially the title, LOL) is absolutely not my original intention. I'll update my question soon. I knew why `"A=132 echo $A" different from "A=132; echo $A"` at the first place! – mitnk Sep 16 '18 at 03:46
  • Then please write a title that **does** encapsulate the question, not just describe what general topic it's related to. – Charles Duffy Sep 16 '18 at 03:49
  • ...if the question is "why doesn't "var=value somecommand" persist the new value for var?", for example, that's a legitimate question (but one which *also* has preexisting answers already in the knowledgebase). – Charles Duffy Sep 16 '18 at 03:50
  • ...see [Setting an environment variable on same line as program execution is different than setting it separately? - shell variables vs. environment variables](https://stackoverflow.com/questions/30180187/setting-an-environment-variable-on-same-line-as-program-execution-is-different-t) for a duplicate valid in *that* interpretation. – Charles Duffy Sep 16 '18 at 03:52
  • @CharlesDuffy Yes. "why doesn't "var=value somecommand" persist the new value for var?" this is what I wanted. And the last link you put is answered my question. – mitnk Sep 16 '18 at 03:55