Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

7
  • How can anything in the command line be expanded after the command runs...? Commented Apr 28, 2014 at 17:13
  • The pre-command assignments to x and y are for the environment that echo runs in, not the environment in which the arguments to echo are expanded. For IFS=, read xx yy zz, the entire string is read, unsplit, by the read command. Then, that string is split according to the value of IFS, with the corresponding pieces assigned to xx, yy, and zz. Commented Apr 28, 2014 at 18:19
  • I just wanted to point out that the wording "expanded before the command runs" doesn't make much sense because after the start of the command nothing is ever expanded any more. Furthermore: Have you not had a single look at my answer or do you nonetheless believe that I need an explanation of what's happening...? Commented Apr 28, 2014 at 19:44
  • 1
    I neither claimed you need an explanation nor that anything can be expanded after the command runs. However, it is true that bash first parses the given command line, recognizes that there are two variable assignments to apply to the environment of the coming command, identifies the command to run (echo), expands any parameters found in the arguments, then runs the command echo with the expanded arguments. Commented Apr 28, 2014 at 19:51
  • In the case of echo I wasn't sure if it could "see" the variable, since it's a builtin command and therefore doesn't run in a subshell which could have its own environment. But I tried it out with eval which also is a builtin and it indeed knows about it. E.g. try a=xyz eval 'echo $BASHPID $a; grep -z ^a /proc/$BASHPID/{,task/*}/environ'; echo $BASHPID $a which shows that a is only set within eval even though the pid is the same and the environment is not altered during eval! (In order to access /proc you need to run this under Linux.) It seems bash does some additional magic here. Commented Nov 20, 2017 at 14:24